Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
Les Les is offline
external usenet poster
 
Posts: 240
Default Macro to extract data and paste to a new sheet

Could anyone please help
I have what you would call a 'typical' spreadsheet i.e. categories across
row 1, dates down column A then a value against certain categories on certain
dates.
What I'm trying to do is create a macro that extracts the data, the date and
and the category to a new work sheet but only where data actually exists.
I'm essentially trying to create a data table from the existing worksheet.
Regards
Les.


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Macro to extract data and paste to a new sheet

Hi Les,

Try something like:

'================
Public Sub CopyTable()
Dim WB As Workbook
Dim SH As Worksheet
Dim destsh As Worksheet
Dim rng As Range
Dim rCell As Range
Dim copyRng As Range
Dim destrng As Range
Dim CalcMode As Long
Dim ViewMode As Long

Set WB = ActiveWorkbook '<<===== CHANGE
Set SH = WB.Sheets("Sheet1") '<<===== CHANGE
Set destsh = WB.Sheets("Sheet2") '<<===== CHANGE
Set destrng = destsh.Range("A1") '<<===== CHANGE

Set rng = SH.Range("A1", Cells(Rows.Count, "A").End(xlUp))

On Error GoTo XIT

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

With ActiveWindow
ViewMode = .View
.View = xlNormalView
End With

SH.DisplayPageBreaks = False

For Each rCell In rng.Cells
If Application.CountA(rCell.EntireRow) 1 Then
If copyRng Is Nothing Then
Set copyRng = rCell
Else
Set copyRng = Union(rCell, copyRng)
End If
End If
Next rCell

If Not copyRng Is Nothing Then
copyRng.EntireRow.Copy Destination:=destrng
Else
'nothing found, do nothing
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With

ActiveWindow.View = ViewMode

End Sub
'<<================


---
Regards,
Norman


"Les" wrote in message
...
Could anyone please help
I have what you would call a 'typical' spreadsheet i.e. categories across
row 1, dates down column A then a value against certain categories on
certain
dates.
What I'm trying to do is create a macro that extracts the data, the date
and
and the category to a new work sheet but only where data actually exists.
I'm essentially trying to create a data table from the existing worksheet.
Regards
Les.




  #3   Report Post  
Posted to microsoft.public.excel.programming
Les Les is offline
external usenet poster
 
Posts: 240
Default Macro to extract data and paste to a new sheet

Hi Norman
Thanks for the help and the very quick response.
I know my way around Excel but I'm afraid I'm still getting used to VB.
Could you tell me what is being said in the 'Set' lines of code where you
are saying 'change'.
Thanks
Les.


"Norman Jones" wrote:

Hi Les,

Try something like:

'================
Public Sub CopyTable()
Dim WB As Workbook
Dim SH As Worksheet
Dim destsh As Worksheet
Dim rng As Range
Dim rCell As Range
Dim copyRng As Range
Dim destrng As Range
Dim CalcMode As Long
Dim ViewMode As Long

Set WB = ActiveWorkbook '<<===== CHANGE
Set SH = WB.Sheets("Sheet1") '<<===== CHANGE
Set destsh = WB.Sheets("Sheet2") '<<===== CHANGE
Set destrng = destsh.Range("A1") '<<===== CHANGE

Set rng = SH.Range("A1", Cells(Rows.Count, "A").End(xlUp))

On Error GoTo XIT

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

With ActiveWindow
ViewMode = .View
.View = xlNormalView
End With

SH.DisplayPageBreaks = False

For Each rCell In rng.Cells
If Application.CountA(rCell.EntireRow) 1 Then
If copyRng Is Nothing Then
Set copyRng = rCell
Else
Set copyRng = Union(rCell, copyRng)
End If
End If
Next rCell

If Not copyRng Is Nothing Then
copyRng.EntireRow.Copy Destination:=destrng
Else
'nothing found, do nothing
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With

ActiveWindow.View = ViewMode

End Sub
'<<================


---
Regards,
Norman


"Les" wrote in message
...
Could anyone please help
I have what you would call a 'typical' spreadsheet i.e. categories across
row 1, dates down column A then a value against certain categories on
certain
dates.
What I'm trying to do is create a macro that extracts the data, the date
and
and the category to a new work sheet but only where data actually exists.
I'm essentially trying to create a data table from the existing worksheet.
Regards
Les.





  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Macro to extract data and paste to a new sheet

Hi Les,

I know my way around Excel but I'm afraid I'm still getting used to VB.
Could you tell me what is being said in the 'Set' lines of code where you
are saying 'change'.



Set WB = ActiveWorkbook '<<===== CHANGE


If the code is to operate on the active workbook, no change is required. If
the code is to operate on the workbook holding the code, change this line
to:

Set WB = ThisWorkbook

If, the code is to operate on another workbook, you will need to provide the
name, e.g.:

Set WB = Workbooks("Les.xls")

where Les.xls is the name of the workbook of interest.

Set destsh = WB.Sheets("Sheet2") '<<===== CHANGE


Replace Sheet2 with the name of the sheet which is to receive the copied
data.


Set destrng = destsh.Range("A1") '<<===== CHANGE


ReplaceA1 with the address of the first cell of the destination range for
the copied data.


---
Regards,
Norman


"Les" wrote in message
...
Hi Norman
Thanks for the help and the very quick response.
I know my way around Excel but I'm afraid I'm still getting used to VB.
Could you tell me what is being said in the 'Set' lines of code where you
are saying 'change'.
Thanks
Les.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
1 Create a macro to Copy & paste certain data to another sheet Lin1981 Excel Discussion (Misc queries) 1 November 6th 08 11:56 PM
macro to extract data of a selected sheet TUNGANA KURMA RAJU Excel Discussion (Misc queries) 1 February 27th 06 06:47 AM
Open Sheet, Run Macro, Extract data CLR Excel Programming 10 November 14th 05 12:53 AM
Macro 2 Extract Data 2 Paste in New Wksht JP2R[_3_] Excel Programming 2 July 7th 05 09:54 PM
macro to extract info and paste to a new sheet Brad K. Excel Programming 1 March 11th 05 10:29 PM


All times are GMT +1. The time now is 03:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"