【问题标题】:Find cell text from one worksheet, in another sheet, then offset with vba从一个工作表中查找单元格文本,在另一个工作表中,然后用 vba 偏移
【发布时间】:2017-05-06 03:52:42
【问题描述】:

我想在我的 VBA 代码中添加一些内容,以便可以从名为“Pull”的工作表、“A”列中名为“HourTracker”的工作表中的单元格“A2”中搜索文本,并向右偏移 1 个单元格。在我找到的这个单元格中,我想粘贴“拉”工作表中单元格“Z1”的内容。

每次运行宏时,“Pull”的内容都会发生变化,单元格“A2”和“Z1”也会发生变化。 “A2”将包含我可以在“HourTracker”表的 A 列中找到的单词,“Pull”表中的“Z1”将包含需要在“HourTracker”中找到的单元格旁边的单元格中的总小时数”。

Sub Function_DataSpecial()

Application.DisplayAlerts = False

Worksheets("Pull").Activate
    Columns("A:BB").Select
    Selection.ClearContents
With ActiveSheet.QueryTables.Add(Connection:="URL;" & 
Sheets("Control").Range("B9").Value, Destination:=Range("A1"))
.Name = "Pull"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlEntirePage
.WebFormatting = xlWebFormattingNone
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
.Refresh BackgroundQuery:=False
End With

Range("A:A").Select
  Selection.TextToColumns _
  Destination:=Range("A1"), _
  DataType:=xlDelimited, _
  TextQualifier:=xlDoubleQuote, _
  ConsecutiveDelimiter:=False, _
  Tab:=False, _
  Semicolon:=False, _
  Comma:=True, _
  Space:=False, _
  Other:=False

ActiveSheet.Range("A:R").RemoveDuplicates Columns:=Array(2, 5), Header:=xlYes

Range("Z1").Select
ActiveCell.FormulaR1C1 = "=SUM(C[-15])"

直到 ActiveSheet.Range("A:R") 部分的所有内容都是从网站中拉出表格,并对表格进行排序,在表格下方我总结了 Hours 列并将总和放入“Z1”。 “HourTracker”工作表中的“A”列不会改变。

我是该网站的新手,所以请告诉我您可能需要帮助的其他信息,谢谢!!!

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    所以经过一番搜索后,我将以下内容添加到我的宏中,并且效果很好。

    Range("Z1").Select
    ActiveCell.FormulaR1C1 = "=SUM(C[-15])"
    Range("Y1").Select
    ActiveCell.FormulaR1C1 = "=R[1]C[-24]"
    
    Range("Z1").Copy
    
    Dim FindString As String
    Dim Rng As Range
    FindString = Sheets("Pull").Range("Y1").Value
    If Trim(FindString) <> "" Then
        With Sheets("HourTracker").Range("A:A")
            Set Rng = .Find(What:=FindString, _
                            After:=.Cells(.Cells.Count), _
                            LookIn:=xlValues, _
                            LookAt:=xlWhole, _
                            SearchOrder:=xlByRows, _
                            SearchDirection:=xlNext, _
                            MatchCase:=False)
            If Not Rng Is Nothing Then
                Application.Goto Rng, True
            Else
                MsgBox "Nothing found"
            End If
        End With
                ActiveCell.Offset(0, 1).Activate
                Selection.PasteSpecial xlPasteValues
    

    我有单元格“Y1”=A2,复制了小时数 (Z1),然后使用 FindString 在“Pull”表上搜索单元格“Y1”的最后一个值。一旦我这样做了,我使用活动单元格偏移来选择它右侧的单元格并粘贴值。

    我希望这可以帮助其他尝试做类似我的事情的人!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      相关资源
      最近更新 更多