• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

want to use VBA for applying concatenate and vlookup to transfer comments from one file to another

Deepak88

New Member
Code:
Public Sub Combine():

Dim strUniqueSubjectIdentifier As String
Dim strVisitname As String
Dim strDateTimeofSpecimencollection As String
Dim strLABTestorExaminationname As String
Dim strReferenceID As String
Dim strAdditionalcomments As String
Dim strCombine As String

strUniqueSubjectIdentifier = Range("A4:A190")
strVisitname = Range("B4:B190")
strDateTimeofSpecimencollection = Range("C4:C190")
strLABTestorExaminationname = Range("D4:D190")
strReferenceID = Range("E4:E190")
strAdditionalcomments = Range("I4:I190")

strCombine = strUniqueSubjectIdentifier & strVisitname & strDateTimeofSpecimencollection & strLABTestorExaminationname & strReferenceID & strAdditionalcomments

Range("J4:J190") = strCombine

End Sub
 

Attachments

  • Sheet1.xlsx
    10.6 KB · Views: 13
  • Sheet2.xlsx
    11.5 KB · Views: 9
Last edited by a moderator:
With no contxt given as to what's going on, I think this is what you want.
Code:
Public Sub Combine()

Application.ScreenUpdating = False
With Range("J4:J190")
    .Formula = "=A4&B4&C4&D4&E4&I4"
    .Copy
    .PasteSpecial xlPasteValues
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
 
With no contxt given as to what's going on, I think this is what you want.
Code:
Public Sub Combine()

Application.ScreenUpdating = False
With Range("J4:J190")
    .Formula = "=A4&B4&C4&D4&E4&I4"
    .Copy
    .PasteSpecial xlPasteValues
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
[/quote


Hi Luke,

Thank you so much for your help. This formula is able to concatenate the cells. Now I want to get comments from sheet 1 into sheet 2 with the help of vlookup macro. CAn yu help me out on this?

Also can you suggest me on this: if there 20 sheets in an excel, all having concatenate macro for every single sheet sicne the number of columns are different, can i make a single shortkey for all the macro? If yes, how?
 
Last edited:
Also can you suggest me on this: if there 20 sheets in an excel, all having concatenate macro for every single sheet sicne the number of columns are different, can i make a single shortkey for all the macro? If yes, how?

Hi Luke,

Thank you so much for your help. This formula is able to concatenate the cells. Now I want to get comments from sheet 1 into sheet 2 with the help of vlookup macro. CAn yu help me out on this?
 
Also can you suggest me on this: if there 20 sheets in an excel, all having concatenate macro for every single sheet sicne the number of columns are different, can i make a single shortkey for all the macro? If yes, how?
Sure thing. Hit Alt+F8 to bring up macro menu, then select macro, options, set your hot key.
upload_2014-11-6_10-38-34.png
Do note you don't really need a macro, you could just use a formula instead, if desired.

For the VLOOKUP, I can try to help. What part are you stuck on? Chandoo has written numerous articles about VLOOKUP and how to use them, they should certainly be able to help you get started.
http://chandoo.org/wp/tag/vlookup/
 
Sure thing. Hit Alt+F8 to bring up macro menu, then select macro, options, set your hot key.
View attachment 12557
Do note you don't really need a macro, you could just use a formula instead, if desired.

For the VLOOKUP, I can try to help. What part are you stuck on? Chandoo has written numerous articles about VLOOKUP and how to use them, they should certainly be able to help you get started.
http://chandoo.org/wp/tag/vlookup/
 
Hey Luke,

Thank you for your help. with regards to vlookup, one the concatenate formula applied to the above attachments using macro, I want to apply vlookup to transfer comments from sheet 1 'L' column to sheet 2 'L' column....also can u help me with how a macro which is writtern in one sheet applicable to another new sheet with same number of cells and columns.
 
The macro I wrote above is not sheet specific. You could run it on any sheet and it would do the exact same range of cells.

For the VLOOKUP, you still didn't state what part you were getting stuck on. Your overall formula needs to follow correct syntax, so will be something like:
=VLOOKUP(CellToFind,TableRange,ColumnNumber,FALSE)
 
The macro I wrote above is not sheet specific. You could run it on any sheet and it would do the exact same range of cells.

For the VLOOKUP, you still didn't state what part you were getting stuck on. Your overall formula needs to follow correct syntax, so will be something like:
=VLOOKUP(CellToFind,TableRange,ColumnNumber,FALSE)
 
Hi Luke,

Appreciate your help...I realy thank u for all your suggestions. Now I have attached 3 sheets...Sheet 1 & 2 have macro for inserting a blank column then combine (concatenation) and vlookup...for vllokup, i need cooments from column M from sheet 1 to column M in sheet 2...Also I want to make sheet 2 as my permanenet macro sheet which can be used for all the open workbooks whenever I open sheet 2. So to test Sheet 2 as a permanent macro, I have attached Sheet3..so if you open both Sheet2 & 3..Sheet3 column L & M should be updated automatically with concatenate and comments respectively. Please suggest me on this
Please find attached sheets and suggest me on this.
 

Attachments

  • Sheet1.xlsm
    20 KB · Views: 12
  • Sheet2.xlsm
    21.7 KB · Views: 7
  • Sheet3.xlsx
    9.1 KB · Views: 4
Hi Deepak88,

Need to clarify on some terminology. Workbooks are individual XL files. Each workbook can hold multiple worksheets. with your above comment, I'm not sure whether you want to have a single workbook hold the macro, or if you want the macro in your Personal.xls workbook, or if you have a single workbook with multiple sheets. Please clarify?
 
Hi Deepak88,

Need to clarify on some terminology. Workbooks are individual XL files. Each workbook can hold multiple worksheets. with your above comment, I'm not sure whether you want to have a single workbook hold the macro, or if you want the macro in your Personal.xls workbook, or if you have a single workbook with multiple sheets. Please clarify?
 
Hi Luke,

Apologies for the confusion. I have one workbook consisting of 25 different worksheets. I receive this kind of workbook every month and I have to apply concatenate & vlookup between the two workbooks manuaaly for every worksheet to transfer the previous comments. Now I want to have a single mast workbook in this way that when I keep the master workbook open with macro for all the worksheets, then it will run very month for the new workbook in order to transfer the comments into it. I hope this will help you to understand my problem. That's the reason I have send you 3 workbooks where you can make 1 (preferably Sheet2) as a master one and test it on Sheet 3. thank you for allyour help & support. :)
 
Hi All,

Anyone please help with below mention VBA code where the code is for filtering subjects in different sheets. However I want to filter subjects in different excel workbooks. Please reply me asap.
Code:
Sub filter_All_Sheets()

Dim objSheet As Worksheet, objMAinSheet As Worksheet
Dim arrAllFilters() As String
Dim byteCountFilter As Byte, i As Byte

   Set objMAinSheet = ActiveSheet
' insert all criteria and address
  If insertAllFilters(arrAllFilters, byteCountFilter) Then
      
       Application.ScreenUpdating = False
' If is allright, go on
    For Each objSheet In ActiveWorkbook.Worksheets
        ' don't do on same sheet
        If objSheet.Name <> objMAinSheet.Name Then
       
          On Error GoTo errhandler
        'check Autofilter, if one is off = switch on
          objSheet.Select
          If Not objSheet.AutoFilterMode Then
             ' if sheet doesn't contain some data
              Range(arrAllFilters(4, 1)).AutoFilter
          End If
         
          ' here I know taht Autofilter is On
          ' filter some item
          For i = 1 To byteCountFilter
          ' only 1 criteria (without Operator)
           If arrAllFilters(2, i) = 0 Then
             Range(arrAllFilters(4, i)).AutoFilter _
                   Field:=Range(arrAllFilters(4, i)).Column, _
                   Criteria1:=arrAllFilters(1, i)
           ' with operator
           ElseIf arrAllFilters(2, i) <> 0 Then
             Range(arrAllFilters(4, i)).AutoFilter _
                   Field:=Range(arrAllFilters(4, i)).Column, _
                   Criteria1:=arrAllFilters(1, i), _
                   Operator:=arrAllFilters(2, i), _
                   Criteria2:=arrAllFilters(3, i)
           End If
          Next i
        
        End If
    Next objSheet
  Else
     'While Main Sheet doesn't contain data or Autofilter is off
     MsgBox "Main Sheet (Name """ & objMAinSheet.Name & """) doesn't some data or it doesn't use !" _
     & vbCrLf & "This code can't go on.", vbCritical, "Missing Autofilter object or filter item "
    
     Set objMAinSheet = Nothing
     Set objSheet = Nothing
   
     Application.ScreenUpdating = True
    
     Exit Sub
  End If
 
    objMAinSheet.Activate
    Set objMAinSheet = Nothing
    Set objSheet = Nothing
   
    Application.ScreenUpdating = True
   
    MsgBox "Finished"
  Exit Sub
 
errhandler:
    Set objMAinSheet = Nothing
    Set objSheet = Nothing
   
    Application.ScreenUpdating = True
   
    If Err.Number = 1004 Then
       MsgBox "Probable cause of error - sheet dosn't contain some data", vbCritical, "Error Exception on sheet " & ActiveSheet.Name
    Else
       MsgBox "Sorry, run exception"
    End If
 
End Sub
Function insertAllFilters(arrAllFilters() As String, byteCountFilter As Byte) As Boolean
' go throught all filters and inserting their address and criterial
Dim myFilter As Filter
Dim myFilterRange As Range
Dim boolFilterOn As Boolean
Dim i As Byte, byteColumn As Byte

  boolFilterOn = False: i = 0: byteColumn = 0
  ' If AutoFilter is off - return False
  If Not ActiveSheet.AutoFilterMode Then
     insertAllFilters = False
     Exit Function
  End If
 
  ' If Autofilter is on & no filter any item = return false
  For Each myFilter In ActiveSheet.AutoFilter.Filters
      If myFilter.On Then
        boolFilterOn = True
        Exit For
      End If
  Next myFilter
  ' Check Filter
  If Not boolFilterOn Then
     insertAllFilters = False
     Exit Function
  End If
  
  On Error GoTo errhandler
' here is all control done
  With ActiveSheet.AutoFilter
    For Each myFilter In .Filters
        byteColumn = byteColumn + 1
        If myFilter.On Then
           i = i + 1
           ReDim Preserve arrAllFilters(1 To 4, 1 To i)
           arrAllFilters(1, i) = myFilter.Criteria1
           arrAllFilters(2, i) = myFilter.Operator
           If myFilter.Operator <> 0 Then
             arrAllFilters(3, i) = myFilter.Criteria2
           End If
           arrAllFilters(4, i) = .Range.Columns(byteColumn).Cells(1).Address
        End If
    Next myFilter
  End With
 
  byteCountFilter = i
  insertAllFilters = True
  Set myFilter = Nothing
  Set myFilterRange = Nothing
Exit Function

errhandler:
insertAllFilters = False
Set myFilter = Nothing
Set myFilterRange = Nothing

End Function
 
Last edited by a moderator:
Looks like the code does the correct thing in referring to ActiveWorkbook and ActiveSheet. Can you tell us what goes wrong? Does the macro crash, or just not do what it's supposed to?
 
Hi Luke,


Are you talking about filter all sheets macro or the one which we were doing earlier related to concatenate and vlookup between sheet 1,2 & 3? Filter macro is working fine with different sheets in a single workbook but not working for different excel workbook.....also please can u reply on the above issue of vlokup and making the macro workbook to be working for every new wokrbook...????
 
Hi, here is a sample of the data,
upload_2015-1-23_12-34-59.png
I don't know how to use the vlookup and concatenate function in VBA.
I need to place the result in E2. read some example but the code gets too complicated... i need to keep it clean and simple.
thanks!!
 
Hi bevg1971,
Please post your question in a new thread, rather than tacking on to an older one. It would also help if you fully explain what result you are looking for in the cell.
 
Back
Top