+ Reply to Thread
Results 1 to 10 of 10

How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each column)

Hybrid View

Amar321 How to Copy data from Sheet 1... 06-22-2016, 06:33 AM
karedog Re: How to Copy data from... 06-22-2016, 12:18 PM
Amar321 Re: How to Copy data from... 06-23-2016, 01:21 AM
karedog Re: How to Copy data from... 06-23-2016, 02:50 AM
Amar321 Re: How to Copy data from... 06-23-2016, 03:18 AM
karedog Re: How to Copy data from... 06-23-2016, 04:00 AM
Amar321 Re: How to Copy data from... 06-23-2016, 04:50 AM
karedog Re: How to Copy data from... 06-23-2016, 07:17 AM
Amar321 Re: How to Copy data from... 06-23-2016, 08:44 AM
Amar321 Re: How to Copy data from... 06-27-2016, 01:02 AM
  1. #1
    Registered User
    Join Date
    06-22-2016
    Location
    Pune
    MS-Off Ver
    2010
    Posts
    14

    How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each column)

    Hello,
    I am new to VBA. I have the data in a sheet 1 with 100+ columns. In sheet 2 i have validation formula(Regular Expressions) for each column. i wants to copy data from sheet 1 to sheet 3 by checking validation formula of each column before pasteing the data in sheet 3. If it satisfies Regex, copy that in sheet 3 and fill the cell with green color or if not satisfies copy data to sheet 3 and fill that cell with red color. Below is images of sheet1,2,3.

    Capture4.JPG
    Capture3.JPG
    Capture2.JPG

    I am not getting idea how to achieve this in VBA.

    Thanks in advance

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Maybe :

    Sub Test()
      Dim arrData, arrCons, arrOk, i As Long, j As Long, m As Long, n As Long, strReport As String
    
      'Get Array of Data
      With Sheets("Sheet1")
        arrData = .Range("A1").CurrentRegion.Value
      End With
    
      'Get Array of Constraint
      With Sheets("Sheet2")
        arrCons = .Range("A1").CurrentRegion.Value
      End With
    
      'Set dimension for Array OK/NG
      ReDim arrOk(1 To UBound(arrData, 1), 1 To 1)
    
      'Convert field name to column number
      For i = 2 To UBound(arrCons, 1)
          For j = 1 To UBound(arrData, 2)
              If arrCons(i, 1) = arrData(1, j) Then
                 arrCons(i, 1) = j
                 Exit For
              End If
          Next j
      Next i
    
      'Warn if there is/are unknown fields
      For i = 2 To UBound(arrCons, 1)
          If Not IsNumeric(arrCons(i, 1)) Then strReport = strReport & "," & arrCons(i, 1)
      Next i
      If Len(strReport) Then
         MsgBox "Unrecognized fields : " & Mid$(strReport, 2)
         Exit Sub
      End If
    
      'Check for length constraint
      For j = 2 To UBound(arrCons, 1)
          m = arrCons(j, 1)
          n = arrCons(j, 3)
          For i = 2 To UBound(arrData, 1)
              If Len(arrData(i, m)) > n Then arrOk(i, 1) = "NG"
          Next i
      Next j
    
      'Check for regular expression
      With CreateObject("VBScript.RegExp")
        .Global = True
        For j = 2 To UBound(arrCons, 1)
            m = arrCons(j, 1)
            .Pattern = arrCons(j, 2)
            For i = 2 To UBound(arrData, 1)
                If arrOk(i, 1) <> "NG" Then
                   If Not .Test(arrData(i, m)) Then arrOk(i, 1) = "NG"
                End If
            Next i
        Next j
      End With
      arrOk(1, 1) = "OK/NG"
      For i = 2 To UBound(arrOk, 1)
          If arrOk(i, 1) <> "NG" Then arrOk(i, 1) = "OK"
      Next i
    
      'Put Result
      With Sheets("Sheet3")
        .Cells.Clear
        With .Range("A1").Resize(UBound(arrData, 1), UBound(arrData, 2) + 1)
          .Value = arrData
          .Columns(.Columns.Count).Value = arrOk
    
          .AutoFilter field:=.Columns.Count, Criteria1:="OK"
          On Error Resume Next
             .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 4
          On Error GoTo 0
          .AutoFilter field:=.Columns.Count, Criteria1:="NG"
    
          On Error Resume Next
             .Offset(1).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Interior.ColorIndex = 3
          On Error GoTo 0
    
          .AutoFilter
        End With
      End With
    End Sub
    Attached Files Attached Files
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    06-22-2016
    Location
    Pune
    MS-Off Ver
    2010
    Posts
    14

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Thank you for answer
    Code is working well but if i change data in Account Number to "asd"(Wrong data) then after checking regular expression it showing OK. i wants to change in code like check first cell in Account Number column if it true (Satisfies Regex) then fill cell with Green color else make it red and similar to all data.
    Simply i wants to check columnwise, first check Account Number Column cell by cell if true make green and false then make Red.
    If one of the cell in whole row contains wrong data then in "OK/NG" Column write NG and if whole row is correct then write it as Ok. plz see the attachment.

    Capture5.JPG

  4. #4
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Code is working well but if i change data in Account Number to "asd"(Wrong data) then after checking regular expression it showing OK.
    Do you understand regular expression syntax ?
    From your screenshot, on Sheet2!B2, the regular expression for "Account Number" is "^[a-zA-Z0-9]+$"
    This means: lowercase a to z, uppercase A to Z, number 0 to 9 should be allowed, but you said "asd" is wrong data ?
    If you want this column is number only, then the regular expression should be "^[0-9]+$" (just the same as for the Bank Code on cell Sheet2!B9)

    Also for "Address", "Bank Name", "Bank Branch", "Bank Address", your regular expression is "^[A-Z0-9\s/\-\?:\(\)\.,'\+]+$"
    Notice that there is no a-z, which means lowercase is forbidden, but from your screenshot on post #3, for cell B2 for example, you mark this cell as green while it should be red (lowercase is forbidden).

    -----------------------------------

    This is the modified code as your request to test and mark individual cell (not whole row) :

    Sub Test()
      Dim cell As Range, arrData, arrCons, arrOk, i As Long, j As Long, m As Long, n As Long, strReport As String
      With Sheets("Sheet1")
        arrData = .Range("A1").CurrentRegion.Value
      End With
      With Sheets("Sheet2")
        arrCons = .Range("A1").CurrentRegion.Value
      End With
      ReDim arrOk(1 To UBound(arrData, 1), 1 To 1)
      For i = 2 To UBound(arrCons, 1)
          For j = 1 To UBound(arrData, 2)
              If arrCons(i, 1) = arrData(1, j) Then
                 arrCons(i, 1) = j
                 Exit For
              End If
          Next j
      Next i
      For i = 2 To UBound(arrCons, 1)
          If Not IsNumeric(arrCons(i, 1)) Then strReport = strReport & "," & arrCons(i, 1)
      Next i
      If Len(strReport) Then
         MsgBox "Unrecognized fields : " & Mid$(strReport, 2)
         Exit Sub
      End If
      For j = 2 To UBound(arrCons, 1)
          m = arrCons(j, 1)
          n = arrCons(j, 3)
          For i = 2 To UBound(arrData, 1)
              If Len(arrData(i, m)) > n Then
                 arrData(i, m) = Chr$(2)
                 arrOk(i, 1) = "NG"
              End If
          Next i
      Next j
      With CreateObject("VBScript.RegExp")
        .Global = True
        For j = 2 To UBound(arrCons, 1)
            m = arrCons(j, 1)
            .Pattern = arrCons(j, 2)
            For i = 2 To UBound(arrData, 1)
                If arrData(i, m) <> Chr$(2) Then
                   If Not .Test(arrData(i, m)) Then
                      arrData(i, m) = Chr$(2)
                      arrOk(i, 1) = "NG"
                   End If
                End If
            Next i
        Next j
      End With
      arrOk(1, 1) = "OK/NG"
      For i = 2 To UBound(arrOk, 1)
          If arrOk(i, 1) <> "NG" Then arrOk(i, 1) = "OK"
      Next i
      With Sheets("Sheet3")
        .Cells.Clear
        Sheets("Sheet1").Range("A1").CurrentRegion.Copy .Range("A1")
        For j = 1 To UBound(arrData, 2)
            For i = 2 To UBound(arrData, 1)
                If arrData(i, j) = Chr$(2) Then
                   .Cells(i, j).Interior.ColorIndex = 3
                Else
                   .Cells(i, j).Interior.ColorIndex = 4
                End If
            Next i
        Next j
        With .Range("A1").CurrentRegion
          With .Offset(, .Columns.Count + 1).Resize(, 1)
            .Value = arrOk
            For Each cell In .Cells
                If cell.Value = "NG" Then
                   cell.Interior.ColorIndex = 3
                Else
                   cell.Interior.ColorIndex = 4
                End If
            Next cell
          End With
        End With
      End With
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    06-22-2016
    Location
    Pune
    MS-Off Ver
    2010
    Posts
    14

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Thank you for answer......sorry i made mistake in asking question but u gave write answer .....Thank you so much

    But if i wants to create new workbook in a folder and copy sheet3 to new sheet in new workbook (wit populated color same as sheet 3) without column OK/NG. Where i should make changes . can i add that code to this code or i should write new code for creating new workbook and copy sheet 3 data to new workbook.

  6. #6
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    You are welcome.

    It's up to you which method you will choose. Here I provide the second method, this Sub Test2() will call Sub Test() and then copy the output to a new workbook.
    If you want the first method, then just add the code of this SubTest2() to the end of Sub Test() (remove the "Call Test" line).

    Sub Test2()
      Dim rng As Range, strLocation As String
    
      Call Test
    
      strLocation = "Z:\Report.xls"
      If Len(Dir(strLocation)) Then Kill strLocation
    
      Set rng = Sheets("Sheet3").Range("A1").CurrentRegion
      With Workbooks.Add
        With .Worksheets.Add
          rng.Copy .Range("A1")
        End With
        .SaveAs Filename:="Z:\Report.xls", FileFormat:=xlWorkbookNormal
        Application.DisplayAlerts = False
          .Close savechanges:=False
        Application.DisplayAlerts = True
      End With
    End Sub

  7. #7
    Registered User
    Join Date
    06-22-2016
    Location
    Pune
    MS-Off Ver
    2010
    Posts
    14

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Thank you very much for your Answer........!!!!!!!!!!!
    Code is Running Properly.........!!!!!!!!!!!

    i wants to make header name in "Report.xls" in Blue Color and all columns should be autofilterd.
    and i wants to store ouput file path is given in sheet4. Where should i make changes.
    Cap.JPG
    Last edited by Amar321; 06-23-2016 at 06:48 AM.

  8. #8
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Sub Test2()
      Dim rng As Range, strLocation As String
    
      Call Test
    
      strLocation = Sheets("Sheet4").Range("B3").Value
      If Right$(strLocation, 1) <> "\" Then strLocation = strLocation & "\"
      strLocation = strLocation & "Report.xls"
      If Len(Dir(strLocation)) Then Kill strLocation
    
      Set rng = Sheets("Sheet3").Range("A1").CurrentRegion
      With Workbooks.Add
        With .Sheets(1)
          rng.Copy .Range("A1")
          With .Range("A1").CurrentRegion
            .EntireColumn.AutoFit
            .Rows(1).Interior.ColorIndex = 41
            .AutoFilter
          End With
        End With
        .SaveAs Filename:=strLocation, FileFormat:=xlWorkbookNormal
        Application.DisplayAlerts = False
          .Close savechanges:=False
        Application.DisplayAlerts = True
      End With
    End Sub

  9. #9
    Registered User
    Join Date
    06-22-2016
    Location
    Pune
    MS-Off Ver
    2010
    Posts
    14

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Thank you code is working very well....!!!!!!!!!!

    But the problem is i have executed it first time the report is file is generated successfully. Without deleting the report.xls file from output path , i tried to execute code second time, the second report.xls file is not get generated but previous file gets overwrited. when i go to execute it for second time it should not overwrite previous file new file should be created.
    Last edited by Amar321; 06-27-2016 at 01:01 AM.

  10. #10
    Registered User
    Join Date
    06-22-2016
    Location
    Pune
    MS-Off Ver
    2010
    Posts
    14

    Re: How to Copy data from Sheet 1 to sheet 3 by using sheet 2 (Contains Regex of each colu

    Hello,
    The problem i am facing is, i have executed it first time the report is file is generated successfully. Without deleting the report.xls file from output path , i tried to execute code second time, the second report.xls file is not get generated but previous file gets overwrited. when i go to execute it for second time it should not overwrite previous file new file should be created.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Macro who copy the data from one sheet to another sheet basis of 2 other column cell value
    By Manish_Gupta in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-23-2015, 12:47 PM
  2. [SOLVED] Macro who copy the data from one sheet to another sheet basis of 2 other column cell value
    By Manish_Gupta in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-23-2015, 09:49 AM
  3. [SOLVED] Macro to Copy data from one sheet to another sheet if column headings match
    By harman83 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 11-04-2015, 02:40 AM
  4. [SOLVED] Copy Column A data to Sheet 2 where rows matches Sheet 2 headers
    By SCDE in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-19-2015, 12:14 AM
  5. Replies: 2
    Last Post: 01-22-2014, 05:36 PM
  6. Copy column data on one sheet to row or column date on another sheet based on user in
    By soready42012 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-08-2012, 12:03 AM
  7. Replies: 1
    Last Post: 10-30-2009, 10:58 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1