繁体   English   中英

在一个工作表中查找单元格文本,在另一工作表中查找,然后用vba偏移

[英]Find cell text from one worksheet, in another sheet, then offset with vba

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

每次运行宏时,“ Pull”的内容都会更改,“ A2”和“ Z1”单元格也会更改。 “ A2”将包含我可以在“ HourTracker”工作表的A列中找到的单词,“拉”工作表中的“ 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”列将保持不变。

我是该网站的新手,所以让我知道您可能还需要其他什么信息,谢谢!!!

因此,在进行一些搜索之后,我将以下内容添加到了我的宏中,它可以正常运行。

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在“拉”表上搜索单元格“ Y1”的最后一个值。 完成此操作后,我将使用活动单元格偏移量来选择其右侧的单元格并粘贴该值。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM