Excel VBA - 选择工作表时出错并检查多个工作表

时间:2015-08-20 08:22:12

标签: excel-vba vba excel

我的代码如下:

Sub Tele()

    Dim rowLoop As Long
    rowLoop = 1
    strValueToFind = Application.InputBox("Enter a Search value in format xx.xx.xxxx, remember that this will only work if you are on 'Tidal' tab", Title:="DATE FIND", Default:=Format(Date, "Short Date"), Type:=1)
    ' Loop column A to find value, number corrosponds to letter position in alphabet
    For rowLoop = 1 To Rows.Count
        If Sheets("2015").Cells(rowLoop, 1).Value = strValueToFind Then ' If value is in C then do something
            ' start on cell found from date needed - look at copying range on same Column
            ' -------------------------------------------------------------------------------------------'
            Sheets("Vessels").Range("C09").Value = Cells(rowLoop, 1).Offset(0, 1).Resize(1).Value
            Sheets("Vessels").Range("C10").Value = Cells(rowLoop, 1).Offset(0, 3).Resize(1).Value
            Sheets("Vessels").Range("C11").Value = Cells(rowLoop, 1).Offset(0, 5).Resize(1).Value
            Sheets("Vessels").Range("C12").Value = Cells(rowLoop, 1).Offset(0, 7).Resize(1).Value
            ' Copy cells 1 cell below found value - Montrose?
            Sheets("Vessels").Range("D09").Value = Cells(rowLoop, 1).Offset(0, 2).Resize(1).Value
            Sheets("Vessels").Range("D10").Value = Cells(rowLoop, 1).Offset(0, 4).Resize(1).Value
            Sheets("Vessels").Range("D11").Value = Cells(rowLoop, 1).Offset(0, 6).Resize(1).Value
            Sheets("Vessels").Range("D12").Value = Cells(rowLoop, 1).Offset(0, 8).Resize(1).Value
            MsgBox ("Found value on col " & rowLoop) '
            Exit Sub
        End If
    Next rowLoop ' This is row number, do something with this

    ' This MsgBox will only show if the loop completes with no success
    MsgBox ("Date not found, make sure you have input the date correctly and on the right tab")

End Sub

所以在这里我们让用户输入一个搜索工作表2015的日期,并根据该位置复制一些单元格。

问题: 如果在不同的工作表上,宏使用该特定工作表上的单元格位置执行一些奇怪的操作 目前代码只能检查一张纸的日期,我需要它检查总共5张,2015年到2020年 我试着在工作表括号内的if语句中用逗号分隔,但我认为这并不容易。
对错误的任何帮助或澄清都会很棒,请提前感谢!

1 个答案:

答案 0 :(得分:0)

需要以两种方式修改代码: -

  1. 为2015-2020的工作表设置循环
  2. 设置对Cells的显式引用,以便它们在正确的工作表上运行
  3. 您仍有逻辑问题。下面的代码将迭代5张(或者你添加到数组中的任何内容),但是每一张都会覆盖`Vessels'工作表上的相同单元格。我怀疑这是你在找什么。

    试试这个(概念中): -

    Sub Tele()
    
        Dim rowLoop As Long
        rowLoop = 1
        strValueToFind = Application.InputBox("Enter a Search value in format xx.xx.xxxx, remember that this will only work if you are on 'Tidal' tab", Title:="DATE FIND", Default:=Format(Date, "Short Date"), Type:=1)
    
        '*** REFACTOR THIS - change how sheets are selected. You haven't said if other sheets exists
        'Select the sheets to work through
        Sheets(Array("2015", "2016", "2017", "2018", "2019", "2020")).Select
    
        For Each ws In ActiveWindow.SelectedSheets
    
          Debug.Print "Checking " & ws.Name
    
          ' Loop column A to find value, number corrosponds to letter position in alphabet
          For rowLoop = 1 To Rows.Count
            If ws.Cells(rowLoop, 1).Value = strValueToFind Then ' If value is in C then do something
              ' start on cell found from date needed - look at copying range on same Column
              ' -------------------------------------------------------------------------------------------'
    
    
              '*** CHANGE THE LOGIC HERE SO SUBSEQUENT SHEETS DON'T OVERWRITE THE VALUES IN "Vessels" ***
              Sheets("Vessels").Range("C09").Value = ws.Cells(rowLoop, 1).Offset(0, 1).Resize(1).Value
              Sheets("Vessels").Range("C10").Value = ws.Cells(rowLoop, 1).Offset(0, 3).Resize(1).Value
              Sheets("Vessels").Range("C11").Value = ws.Cells(rowLoop, 1).Offset(0, 5).Resize(1).Value
              Sheets("Vessels").Range("C12").Value = ws.Cells(rowLoop, 1).Offset(0, 7).Resize(1).Value
              ' Copy cells 1 cell below found value - Montrose?
              Sheets("Vessels").Range("D09").Value = ws.Cells(rowLoop, 1).Offset(0, 2).Resize(1).Value
              Sheets("Vessels").Range("D10").Value = ws.Cells(rowLoop, 1).Offset(0, 4).Resize(1).Value
              Sheets("Vessels").Range("D11").Value = ws.Cells(rowLoop, 1).Offset(0, 6).Resize(1).Value
              Sheets("Vessels").Range("D12").Value = ws.Cells(rowLoop, 1).Offset(0, 8).Resize(1).Value
              MsgBox ("Found value on col " & rowLoop) '
              Exit Sub
            End If
          Next rowLoop ' This is row number, do something with this
    
        'Back for next sheet
        Next ws
    
        ' This MsgBox will only show if the loop completes with no success
        MsgBox ("Date not found, make sure you have input the date correctly and on the right tab")
    
    End Sub