Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops Excel VBA查找/替换循环-在VBA中非常新_Loops_Excel_Replace_Vba - Fatal编程技术网

Loops Excel VBA查找/替换循环-在VBA中非常新

Loops Excel VBA查找/替换循环-在VBA中非常新,loops,excel,replace,vba,Loops,Excel,Replace,Vba,我需要在现有宏中添加一个部分,该部分接受单元格的地址,并从工作表其他位置的单元格范围内的值中查找该地址(作为字符串?),然后偏移一列,以使用该单元格的值替换搜索地址的单元格的原始值 我的代码正在寻找未合并的单元格,当它找到未合并的单元格时,它需要获取正确的值以放入其中。并非我的范围内的所有单元格都是未合并的,因此这是一个循环中的查找/替换 我无法硬编码单元格,也无法找出一个函数循环,该循环可以成功地在我的范围内移动,并使用工作表另一部分的值查找/替换。我是VBA新手,经常出错,最后定义了十几个范

我需要在现有宏中添加一个部分,该部分接受单元格的地址,并从工作表其他位置的单元格范围内的值中查找该地址(作为字符串?),然后偏移一列,以使用该单元格的值替换搜索地址的单元格的原始值

我的代码正在寻找未合并的单元格,当它找到未合并的单元格时,它需要获取正确的值以放入其中。并非我的范围内的所有单元格都是未合并的,因此这是一个循环中的查找/替换

我无法硬编码单元格,也无法找出一个函数循环,该循环可以成功地在我的范围内移动,并使用工作表另一部分的值查找/替换。我是VBA新手,经常出错,最后定义了十几个范围和字符串,试图传递数据。任何帮助都将不胜感激

例如:

如果unmerged mCell.address=“B20”,则宏会在指定范围内找到值“B20”(在下面的示例中,是在单元格Q20中找到的),然后偏移一列(到单元格R20),然后使用该单元格的值(即6)替换mCell的值,这样B20的新单元格值(即活动mCell)=6。然后是下一个未合并的mCell

 row   Column Q   Col. R '(not code, but can't get formatting any other way)
 18    B18(text)  5
 19    B19        4
 20    B20        6
 21    B21        3
谢谢你的建议,我现有的代码在“第二部分”之前都很好用,但是我失败得很惨,我正在寻求关于如何纠正/改进代码的具体帮助。现行守则如下:

    ' This sub looks for the word "Table" in column A.  If the word appears, it unmerges the  cells in columns B - E
    ' and the rows following to allow for the insert of a table, then merges all other rows for sake of format.

Option Explicit
Private Sub Worksheet_Activate()

Application.ScreenUpdating = False

Range("B14:E64").SpecialCells(xlCellTypeVisible).Select
With Selection
.RowHeight = 17
.VerticalAlignment = xlTop
.HorizontalAlignment = xlLeft
.WrapText = True
End With

'*******Merge or unmerge rows according to whether or not they contain Table data -
' this only acts on visible cells, so rows of data table can be hidden as needed

 Dim TA As Integer
 Dim ColValues As Variant
 Dim rng As Range
 Dim tabNo As Range                    'uses value on worksheet to know how many rows to unmerge

'*******Dims in finding and replacing unmerged cell values

 Dim mergeRange As Range             'Range B16:E64 - where my mCells are being pulled from
 Dim mCell As Range                  'Cell that is unmerged, looking for its address
 Dim ws As Worksheet
 Dim tabledata As Range              'Range Q11:Q38 - this is the column I'm searching in and offsetting from
 Dim aCell As String                 'picks up cell address, to use in .find
 Dim myCell As Range                 'cell address in Q
 Dim ReplaceString As String
 Dim foundCell As Range
 Dim bCell As Range
 Dim i As Long

Application.DisplayAlerts = False

'Make column B = Column A values, cannot make this happen on sheet, because data is too variable

ColValues = ActiveSheet.Range("A16:A64").Value
ActiveSheet.Range("B16:B64").Value = ColValues

'Look for data table, if not present, merge cells
Set rng = ActiveSheet.Range("B14:B100")
Set tabNo = ActiveSheet.Range("K6")

For TA = 15 To 64                     'defines TA variable to loop from row 14 to row 64

If Cells(TA, "A") = "Table" Then      '

Range("B" & TA & ":E" & TA + tabNo).UnMerge   'unmerges the row with "Table" listed and the next 7 rows (to make a 8-row x 4 column unmerged area for table
TA = TA + tabNo                               ' moves active cell "TA" down 7 spaces


  Else

Range("B" & TA & ":E" & TA).Merge         'If "Table" not found, then merge the cells for the row TA is in across columns B:E
  End If

Next TA


'*** Part II: Need some calculation to loop or offset or find through data and fill
'unmerged cells from a data table on the worksheet.
'the placement of the data table varies depending on the layout of the report,
'which changes day to day, so can not be hard coded into the cells - needs to look up
'position of the word "Table" and dump data after that.

'offset? .find? loop?


'***want to take the cell address of each unmerged cell within the range of the report
'and look for that cell in an array, then replace the cell contents with the correct value


Set mergeRange = ActiveSheet.Range("B16:E64")

For Each mCell In mergeRange
   ' If mergeRange.MergeCells = True Then
   ' MsgBox "all cells are merged, exiting sub"
   ' Exit Sub
   'Else
    If mCell.MergeCells = False Then

   aCell = mCell.Address      '??? Need to set the cell address as
                                    'a text string or something in order to look for that address in the values
                                  'of cells in range "tabledata"

    'MsgBox "aCell " & Range(aCell).Address


    Set tabledata = ActiveSheet.Range("Q11:Q38")

    Set bCell = tabledata.Find(aCell, After:=Range("Q1"), LookIn:=xlValues, lookAt:=xlWhole,  SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)

                                    'this gives me a "type mismatch" error that I cannot clear


                                    '- then wanting the value of the cell offset one column over
                                    'need to take the value of that offset cell and use it
                                    'to replace the value of the original unmerged cell (mCell)

    ActiveCell.Offset(1, 0).Select
    ActiveCell.Offset(0, 1).Value = ActiveCell.Value



  Application.DisplayAlerts = True


  Application.ScreenUpdating = True

  End If
  Next mCell

  End Sub

那里有一些问题,但我认为它现在起作用了。你必须核实一下,因为我还不能100%确定它应该做什么

问题1:您不需要
tabledata
。您可以在搜索参数
中指定After:=Range(“Q1”)
以使其位于正确的位置<代码>查找在
单元格上工作
因此您的行应该是:

Set bCell = Cells.Find(aCell, After:=Range("Q1"), LookIn:=xlValues, lookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
问题2:您的行
aCell=mCell.Address
需要是
aCell=Replace(mCell.Address,“$”,“”)
,因为它是作为绝对单元格引用输入的,而工作表上的单元格地址不是(可能是一种更优雅的方法)

Dropbox文件中还存在一些其他问题,但现在也应该对这些问题进行排序。有一个额外的
Next
aCell.Offset(,1)=bCell.Offset(,1)
看起来应该是
mCell.Offset(,1)=bCell.Offset(,1)


里面有很多问题。把这个问题分成几个小问题也许是个好主意。杰米,这是一篇很长的帖子,但我的主要目标是一个好的循环。我认为我的“aCell”中没有正确携带手机地址,无法通过我的.Find语句。我试着自己编写代码,但没有走多远。我的excel文件在Dropbox上,如果有人想看看它是如何工作的:事实上,你们非常接近。如果您还没有找到答案,请参阅下面的答案。