首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将单元格格式化为vba中的数字

将单元格格式化为vba中的数字
EN

Stack Overflow用户
提问于 2022-03-16 19:44:07
回答 1查看 32关注 0票数 0

我目前正在从一个软件导出一个文档(wsCopy),并将数据的特殊值粘贴到另一个正在创建(wsDest)的excel工作簿中。在(wsDest)中,我还有两个查询,分别是AI:AJ列(引用新创建的导出数据)、一个"SupportReference“选项卡和一个"RegionLookup”选项卡。我的问题是导出数据中的值是奇怪地格式化的。即使导出的数据格式显示“通用”,列AJ中的查找仍返回#N/A错误。解决这个问题的唯一方法是,如果我单击引用的单元格(列AH),其中包括导出的数据,点击F2,然后输入或创建一个vba,它将使列AI中的所有查找量乘以1。但是,我不想做第一个选项,因为我必须对大约14000行这样做。第二个选项的问题是,如果AI列中的vlookup以任何字母返回,则将整个AI列乘以1将无法工作;此外,它将去掉列AI中的vlookup。下面是我想出的:

代码语言:javascript
运行
复制
Sub CopyOver()

Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long

  Set wsCopy = Workbooks("Export").Worksheets("Sheet1")
  Set wsDest = ThisWorkbook.Worksheets("Data")
  
    '1. Find last used row in the copy range based on data in column A
    lCopyLastRow = wsCopy.Cells(wsCopy.Rows.Count, "A").End(xlUp).Row
      
    '2. Find first blank row in the destination range based on data in column A
    'Offset property moves down 1 row
    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
    
    '3. Clear contents of existing data range
    wsDest.Range("A9:AH" & lDestLastRow).ClearContents

    '4. Copy & Paste Data
    wsCopy.Range("A2:AH" & lCopyLastRow).SpecialCells(xlCellTypeVisible).Copy
    wsDest.Range("A9").PasteSpecial Paste:=xlPasteValues

'Start copy down formulas
Dim DestLastPopulatedRow As Long
'Find last populated row
DestLastPopulatedRow = wsDest.Range("A" & Rows.Count).End(xlUp).Row
'Select the rows where formula is to be populated
wsDest.Range("AI9: " & "AJ" & DestLastPopulatedRow).FillDown

End Sub

代码(wsDest.Range("AI9: " & "AJ" & DestLastPopulatedRow).FillDown)的最后一部分就是我开始出现问题的地方,因为vlookup公式是从单元格AI9开始的。vlookup公式如下:

列AI Vlookup:=VLOOKUP(AH9,'SupportReference'!E:E,1,FALSE)

列AJ Vlookup:=VLOOKUP(AI9,'RegionLookup'!M:M,1,FALSE)

尝试使用VBA代码乘列AI:

代码语言:javascript
运行
复制
With wsDest.Range("AI9: " & "AI" & DestLastPopulatedRow)
    .Value = Evaluate(.Address & "*1")
End With

如果您需要更多的澄清或进一步的数据,请告诉我。

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2022-03-16 23:18:07

如果您不需要VLOOKUP函数是活动的,我希望是这样的,因为您正在复制大量数据块,您可以使用VBA来设置查找单元格的值,而不是函数。下面是一个子过程,将其传递给目标单元格和其他查找参数,VBA查找值并将其放入目标单元格中:

代码语言:javascript
运行
复制
Sub exact_lookup(destination As Range, lookup_value As Variant, lookup_table As Range, column As Integer)
    Dim foundCell As Range
    Set foundCell = lookup_table.Columns(1).Find(lookup_value, , , xlWhole)
    
    If foundCell Is Nothing Then
        destination.Value = CVErr(xlErrNA)
    Else
        destination.Value = foundCell.Offset(0, column - 1).Value
    End If
    
End Sub

然后编写一个循环来填充当前方法使用查找公式的每个单元格的值。一个例子是:

代码语言:javascript
运行
复制
exact_lookup wsDest.range("AI9"), wsDest.range("AH9").value, worksheets("RegionLookup").range("E:E"), 1

注意:我不太确定我是否理解你在做什么,但希望这种方法能奏效--这足以让我朝着正确的方向前进。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71503372

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档