User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Visual Basic 4 / 5 / 6 section within the Software Development category of DaniWeb, a massive community of 391,929 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,717 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Visual Basic 4 / 5 / 6 advertiser:
Views: 1154 | Replies: 3
Reply
Join Date: Jul 2008
Posts: 2
Reputation: TTF is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TTF TTF is offline Offline
Newbie Poster

[Excel Macros] Creating Macros to search and copy columns

  #1  
Jul 9th, 2008
I have a spreadsheet that has many columns. I want to search the titles of the columns and copy all the content in the column that satifies the search criterion and paste it in different worksheet. Copying these columns manually is very time consuming and I wanted to create Macro that automate searching (based on their column title), copying (all contents in the column) and pasting (to a separate worksheet).

Any help is appreciated.

Thanks,

<TTF>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 76
Reputation: manoshailu is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
manoshailu's Avatar
manoshailu manoshailu is online now Online
Junior Poster in Training

Re: [Excel Macros] Creating Macros to search and copy columns

  #2  
34 Days Ago
hi,

Try the below code.



Sub copycolumns()

'
' copycolumns Macro
' Variable Declaration
'    
    
    Dim strColName As String
    Dim intRng As Integer
    Dim i as Integer 
    Dim strVal As String
    
    intRng = InputBox("Enter the No. of Columns?", "No. of columns")  'To get the No. of Columns Available to Search
    strColName = InputBox("Enter the Column Name to Copy?", "Column Name") 'To Get the Column Name to Search 
    strSheetName = InputBox("Enter the Sheet Name to Paste?", "Sheet Name")'To get the Sheet Name to paste the content 

    For i = 1 To intRng
       'Store the Cell Value
        strVal = Cells(1, i)
        'Check the Value with the User given column name
        If UCase(strVal) = UCase(strColName) Then
'Select and Copy
            Cells(1, i).Select
            Range(Selection, Selection.End(xlDown)).Select
            Selection.Copy

'Select and Paste
            Sheets(strSheetName).Select
            Range("A1").Select
            ActiveSheet.Paste

        End If
    Next
End Sub


Shailaja


Originally Posted by TTF View Post
I have a spreadsheet that has many columns. I want to search the titles of the columns and copy all the content in the column that satifies the search criterion and paste it in different worksheet. Copying these columns manually is very time consuming and I wanted to create Macro that automate searching (based on their column title), copying (all contents in the column) and pasting (to a separate worksheet).

Any help is appreciated.

Thanks,

<TTF>
Reply With Quote  
Join Date: Jul 2008
Posts: 2
Reputation: TTF is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
TTF TTF is offline Offline
Newbie Poster

Re: [Excel Macros] Creating Macros to search and copy columns

  #3  
28 Days Ago
Thanks Shailaja,
the code works to copy and paste a single column. is there a way to make a multiple selection (columns) and paste them in a separate worksheet.

thanks,
TTF
Reply With Quote  
Join Date: Jun 2007
Posts: 76
Reputation: manoshailu is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 6
manoshailu's Avatar
manoshailu manoshailu is online now Online
Junior Poster in Training

Re: [Excel Macros] Creating Macros to search and copy columns

  #4  
28 Days Ago
hi

Try this select no. of columns from one sheet to another.


Sub copycolumns()

'
' copycolumns Macro
' Variable Declaration
'        

    Dim strColRng As String
    Dim strSheetName As String
    Dim intNoofCols As Integer
    Dim strColName() As String
    Dim strCurSheetName As String
    
    'To get the No. of Columns Available to Search
    intRng = InputBox("Enter the No. of Columns?", "No. of columns")  

    'To get the No. of Columns to copy and paste
    intNoofCols = InputBox("Enter the No. of Columns to Copy and Paste?", "Copy and Paste")

    'To set size of the Array
    ReDim Preserve strColName(intNoofCols)

    For i = 0 To intNoofCols - 1
        'To Get the Column Name to Search       
        strColName(i) = InputBox("Enter the Column Name to Copy?", "Column Name")
    Next

   'To get the Sheet Name to paste the content  
    strSheetName = InputBox("Enter the Sheet Name to Paste?", "Sheet Name")

    'To store the Current Sheet Name where to copy  

    strCurSheetName = ActiveSheet.Name

    For j = 0 To intNoofCols - 1 'To get the Column Names from the Array

          For i = 1 To intRng
           
                'To Select the Sheet which column to copy
                Sheets(strCurSheetName).Select  
                
                  'Store the Cell Value     
                strVal = Cells(1, i)

                'Check the Value with the User given column name
                If UCase(strVal) = UCase(Trim(strColName(j))) Then
            
                        'Select and Copy            
                         Cells(1, i).Select
                         Range(Selection, Selection.End(xlDown)).Select
                         Selection.Copy

                        'Select and Paste            
                        Sheets(strSheetName).Select
                        Cells(1, j + 1).Select
                        Range(Selection, Selection.End(xlDown)).Select
                        ActiveSheet.Paste

                 End If
          Next
     Next
End Sub

Shailaja

Originally Posted by TTF View Post
Thanks Shailaja,
the code works to copy and paste a single column. is there a way to make a multiple selection (columns) and paste them in a separate worksheet.

thanks,
TTF
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Visual Basic 4 / 5 / 6 Marketplace
Thread Tools Display Modes

Other Threads in the Visual Basic 4 / 5 / 6 Forum

All times are GMT -4. The time now is 8:25 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC