VBA copy into another sheet with similar name

quej2003

New Member
Joined
Oct 5, 2010
Messages
49
Hi

So I have some data in column B in one sheet that is labelled "Jan 2011.txt" and I am looking to copy it into column A of a sheet named "Jan 2011 Data". I have this for quite a few sheets/months, was wondering how in VBA it is written that we take the 'Jan 2011' and then add the 'Data', so that can be used in a bigger macro that goes through a few of these sorts of sheets? Basically the naming!

Cheers
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
quej2003,

What version of Excel are you using?

You will generally get much more help (and faster) in this forum if you can post your small samples directly in the forum.

Please attach screenshots of your workbook or a sample workbook that accurately portrays your current workbook on one sheet, and what it should look like 'After' on another sheet.

This makes it much easier to see exactly what you want to do, as well as shows us whether there is a consistent number of rows between tables and such.

Here are three possible ways to post small (copyable) screen shots directly in your post:

Please post a screenshot of your sheet(s), what you have and what you expect to achieve, with Excel Jeanie HTML 4 (contains graphic instructions).
http://www.excel-jeanie-html.de/html/hlp_schnell_en.php

or
RichardSchollar’s beta HTML Maker -...his signature block at the bottom of his post

or
Borders-Copy-Paste
 
Upvote 0
quej2003,

What version of Excel are you using?

You will generally get much more help (and faster) in this forum if you can post your small samples directly in the forum.

Please attach screenshots of your workbook or a sample workbook that accurately portrays your current workbook on one sheet, and what it should look like 'After' on another sheet.

This makes it much easier to see exactly what you want to do, as well as shows us whether there is a consistent number of rows between tables and such.

Here are three possible ways to post small (copyable) screen shots directly in your post:

Please post a screenshot of your sheet(s), what you have and what you expect to achieve, with Excel Jeanie HTML 4 (contains graphic instructions).
http://www.excel-jeanie-html.de/html/hlp_schnell_en.php

or
RichardSchollar’s beta HTML Maker -...his signature block at the bottom of his post

or
Borders-Copy-Paste

Ok have had a go with the Excel Jeanie HTML 4, on a side note am using excel 2007 at the moment. So here goes:


Excel Workbook
AB
1*Data
2*1
3*2
4*4
5*8
6*16
7*32
8*64
9*128
Jan 2011.txt


which is then copied into:


Excel Workbook
AB
1Date here*
21*
32*
44*
58*
616*
732*
864*
9128*
Jan 2011 Data


Not too sure why I have some stars in the first, but close enough for a first go! Also minor bump
 
Upvote 0
quej2003,


Sample raw data:


Excel Workbook
B
1Data
21
32
44
58
616
732
864
9128
10
Jan 2011.txt





Excel Workbook
B
1Data
22
34
48
516
632
764
8128
9256
10
Feb 2011.txt





After the macro:


Excel Workbook
A
1Jan 2011
21
32
44
58
616
732
864
9128
10
Jan 2011 Data





Excel Workbook
A
1Feb 2011
22
34
48
516
632
764
8128
9256
10
Feb 2011 Data





Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).


1. Copy the below code, by highlighting the code and pressing the keys CTRL + C
2. Open your workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.


Code:
Option Explicit
Sub CopyData()
' hiker95, 03/21/2011
' http://www.mrexcel.com/forum/showthread.php?t=537420
Dim ws As Worksheet, N As String, wsN As Worksheet
Dim LR As Long, NR As Long
Application.ScreenUpdating = False
For Each ws In ThisWorkbook.Worksheets
  If Right(ws.Name, 4) = ".txt" Then
    N = Left(ws.Name, Len(ws.Name) - 4) & " Data"
    If SheetExists(N) = False Then Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = N
    Set wsN = Worksheets(N)
    With wsN.Range("A1")
      .NumberFormat = "@"
      .Value = Left(ws.Name, Len(ws.Name) - 4)
    End With
    LR = ws.Cells(Rows.Count, 2).End(xlUp).Row
    NR = wsN.Range("A" & Rows.Count).End(xlUp).Offset(1).Row
    ws.Range("B2:B" & LR).Copy wsN.Range("A" & NR)
    wsN.Columns(1).HorizontalAlignment = xlCenter
  End If
Next ws
Worksheets(1).Activate
Application.ScreenUpdating = True
End Sub


Function SheetExists(SheetName As String) As Boolean
' http://sculpt.wordpress.com/2007/01/22/check-a-worksheet-exists-in-a-specified-workbook/
' True if worksheet exists in selected workbook
SheetExists = False
On Error GoTo NoSuchSheet
If Len(Sheets(SheetName).Name) > 0 Then
  SheetExists = True
  Exit Function
End If
NoSuchSheet:
End Function



Make sure you save your workbook, Save As, a macro enabled workbook with file extension .xlsm


Then run the CopyData macro.
 
Upvote 0

Forum statistics

Threads
1,224,587
Messages
6,179,736
Members
452,940
Latest member
Lawrenceiow

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top