复制并粘贴“IF”

时间:2017-10-03 23:19:26

标签: vba excel-vba excel

我正在尝试创建一个代码:

  1. 复制输入(如果不是空白(来自输入页面)
  2. 如果它们满足一组索引(State + Segment + Fuel组合),则粘贴到输出数据库上的相应行
  3. 输入页面如下所示。如果黄色框为空白,则跳过该步骤,如果没有,则复制并粘贴到另一个名为“数据”的页面

    enter image description here

    你们有机会帮忙吗?我的代码都错了(我是初学者):(

         Sub UpliftDelta()
    
         Dim ws As Worksheet, inputs As Worksheet, datatbl As Worksheet, ws1 As 
     Worksheet
         Dim c As Integer, i As Integer, j As Integer
         Dim Tariffname As String
    
         Set ws = Sheets("General")
         Set ws1 = Sheets("StateSegFuel")
         Set inputs = Sheets("Inputs")
         Set datatbl = Sheets("Data")
         Set Lookup_Range = inputs.Range("P1:R57")
    
         a = ws1.Cells(Rows.Count, 1).End(xlUp).Row 'bottom most with numbers
         b = datatbl.Cells(Rows.Count, 1).End(xlUp).Row 'bottom most with numbers
    
         MsgBox a
         MsgBox b
    
         i = 8 'row in interface
         j = 3 'column in interface
         r = 1 'row in data
         c = 0 'column in data
    
         For i = 8 To a
    
    If ws1.Cells(i, j - 1).Value = "PeakPrice1" Or ws1.Cells(i, j - 1).Value = "OffPeakPrice1" Or ws1.Cells(i, j - 1).Value = "ShoulderPrice1" Or ws1.Cells(i, j - 1).Value = "PeakBlock1" Then
    
        If ws1.Cells(i, j).Value <> "" Then
            Tariffname = ws1.Cells(i, j - 1).Value 'grab tariff name
            ws1.Range(Cells(i, j), Cells(i, j + 5)).Copy 'copy range of numbers
            c = Application.WorksheetFunction.VLookup(Tariffname, Lookup_Range, 3, False) 'grab column number of tariff
    
                            If datatbl.Cells(r, 7).Value = ws1.Range("H5") Then
                                datatbl.Activate
                                datatbl.Cells(r, c).PasteSpecial xlPasteValues 'paste inputs on Data table
    
                            End If
        End If
        i = i + 1
    
    ElseIf ws1.Cells(i, j - 1).Value = "ShoulderBlock1" Or ws1.Cells(i, j - 1).Value = "OffPeakBlock1" Then
    
        If ws1.Cells(i, j).Value <> "" Then
            Tariffname = ws1.Cells(i, j - 1).Value 'grab tariff name
            ws1.Range(Cells(i, j), Cells(i, j + 4)).Copy 'copy range of numbers
            c = Application.WorksheetFunction.VLookup(Tariffname, Lookup_Range, 3, False) 'grab column number of tariff
    
                            If datatbl.Cells(r, 7).Value = ws1.Range("H5") Then
                                datatbl.Activate
                                datatbl.Cells(r, c).PasteSpecial xlPasteValues 'paste inputs on Data table
    
                            End If
        End If
        i = i + 1
    
    
    ElseIf ws1.Cells(i, j).Value <> "" Then
            Tariffname = ws.Cells(i, j - 1).Value 'grab tariff name
            ws1.Cells(i, j).Copy 'copy range of numbers
            c = Application.WorksheetFunction.VLookup(Tariffname, Lookup_Range, 3, False) 'grab column number of tariff
    
                            If datatbl.Cells(r, 7).Value = ws1.Range("H5") Then
                                datatbl.Activate
                                datatbl.Cells(r, c).PasteSpecial xlPasteValues 'paste inputs on Data table
                            End If
                            i = i + 1
        Loop
    
    
    
       End If
       r = r + 1
      Loop
    
      MsgBox "Saved"
      End Sub
    

0 个答案:

没有答案