Help to correct code

P

Pat

Could someone let me know what is wrong with the following two pieces of
code?

Private Sub Test_Click()
Dim LastRow As Long
Dim FirstRow As Long
Dim StartRange As Range

'Find end of data in col A in Sheet1
LastRow = LastDataRow(ThisWorkbook.Sheets("Sheet1").Range("A:A"))
With ThisWorkbook.Sheets("Sheet1")
'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row
Set StartRange = .Range("A1:G1")
End With
StartRange.Resize(LastRow - FirstRow + 1).FillDown
Calculate
ThisWorkbook.Sheets("Sheet1").SaveAs _
Filename:="C:\MyDocumets\Excel examples\ReadyTest.CSV",
FileFormat:=xlCSV
Application.DisplayAlerts = True
Windows("PrepareTest.xls").Activate
Range("D1").Select

Calculate

End Sub


Option Explicit

Function LastDataRow(Col As Range) As Long
'Finds last row containing data in column defined by Col
Dim iCol As Integer
iCol = Col.Column
LastDataRow = Col.Columns(1).Cells(65526).End(xlUp).Row
Do While Cells(LastDataRow, iCol) = 0 Or _
Trim(Cells(LastDataRow, iCol)) = "" _
And LastDataRow > 1
LastDataRow = LastDataRow - 1
Loop
End Function

If it isn't clear what I am trying to achieve I will elaborate further.

Thanks
Pat
 
B

Bob Phillips

I think it might help if you give us a hint as to what is going on or not as
the case may be, rather than leave us to work it out.

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
P

Pat

Sorry for the vagueness of my question.

The code is placed in a workbook called PrepareTest.xls

I am trying to filldown the following formulas which are on the first row.
The filling down must stop when there is no more data to display in column A

='[Master.xls]PL'!$D8 contained in A1
='[Master.xls]PL'!$E8 contained in B1
='[Master.xls]PL'!$F8 contained in C1
='[Master.xls]PL'!$G8 contained in D1
='[Master.xls]PL'!$H8 contained in E1
='[Master.xls]PL'!$I8 contained in F1
='[Master.xls]PL'!$J8 contained in G1

Then I want to save the workbook as a .CSV file

Hope you understand what I am trying to do.


Bob Phillips said:
I think it might help if you give us a hint as to what is going on or not as
the case may be, rather than leave us to work it out.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Pat said:
Could someone let me know what is wrong with the following two pieces of
code?

Private Sub Test_Click()
Dim LastRow As Long
Dim FirstRow As Long
Dim StartRange As Range

'Find end of data in col A in Sheet1
LastRow = LastDataRow(ThisWorkbook.Sheets("Sheet1").Range("A:A"))
With ThisWorkbook.Sheets("Sheet1")
'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row
Set StartRange = .Range("A1:G1")
End With
StartRange.Resize(LastRow - FirstRow + 1).FillDown
Calculate
ThisWorkbook.Sheets("Sheet1").SaveAs _
Filename:="C:\MyDocumets\Excel examples\ReadyTest.CSV",
FileFormat:=xlCSV
Application.DisplayAlerts = True
Windows("PrepareTest.xls").Activate
Range("D1").Select

Calculate

End Sub


Option Explicit

Function LastDataRow(Col As Range) As Long
'Finds last row containing data in column defined by Col
Dim iCol As Integer
iCol = Col.Column
LastDataRow = Col.Columns(1).Cells(65526).End(xlUp).Row
Do While Cells(LastDataRow, iCol) = 0 Or _
Trim(Cells(LastDataRow, iCol)) = "" _
And LastDataRow > 1
LastDataRow = LastDataRow - 1
Loop
End Function

If it isn't clear what I am trying to achieve I will elaborate further.

Thanks
Pat
 
P

Pat

I need to make a correction, instead of Master.xls in each of the formulas
it should be PrepareTest.xls.
Sorry

Pat said:
Sorry for the vagueness of my question.

The code is placed in a workbook called PrepareTest.xls

I am trying to filldown the following formulas which are on the first row.
The filling down must stop when there is no more data to display in column A

='[Master.xls]PL'!$D8 contained in A1
='[Master.xls]PL'!$E8 contained in B1
='[Master.xls]PL'!$F8 contained in C1
='[Master.xls]PL'!$G8 contained in D1
='[Master.xls]PL'!$H8 contained in E1
='[Master.xls]PL'!$I8 contained in F1
='[Master.xls]PL'!$J8 contained in G1

Then I want to save the workbook as a .CSV file

Hope you understand what I am trying to do.


Bob Phillips said:
I think it might help if you give us a hint as to what is going on or
not
as
the case may be, rather than leave us to work it out.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Pat said:
Could someone let me know what is wrong with the following two pieces of
code?

Private Sub Test_Click()
Dim LastRow As Long
Dim FirstRow As Long
Dim StartRange As Range

'Find end of data in col A in Sheet1
LastRow = LastDataRow(ThisWorkbook.Sheets("Sheet1").Range("A:A"))
With ThisWorkbook.Sheets("Sheet1")
'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row
Set StartRange = .Range("A1:G1")
End With
StartRange.Resize(LastRow - FirstRow + 1).FillDown
Calculate
ThisWorkbook.Sheets("Sheet1").SaveAs _
Filename:="C:\MyDocumets\Excel examples\ReadyTest.CSV",
FileFormat:=xlCSV
Application.DisplayAlerts = True
Windows("PrepareTest.xls").Activate
Range("D1").Select

Calculate

End Sub


Option Explicit

Function LastDataRow(Col As Range) As Long
'Finds last row containing data in column defined by Col
Dim iCol As Integer
iCol = Col.Column
LastDataRow = Col.Columns(1).Cells(65526).End(xlUp).Row
Do While Cells(LastDataRow, iCol) = 0 Or _
Trim(Cells(LastDataRow, iCol)) = "" _
And LastDataRow > 1
LastDataRow = LastDataRow - 1
Loop
End Function

If it isn't clear what I am trying to achieve I will elaborate further.

Thanks
Pat
 
P

Pat

O dear, I think I have eaten too much turkey, please ignore the last post
the original is correct after all.


Pat said:
I need to make a correction, instead of Master.xls in each of the formulas
it should be PrepareTest.xls.
Sorry

Pat said:
Sorry for the vagueness of my question.

The code is placed in a workbook called PrepareTest.xls

I am trying to filldown the following formulas which are on the first row.
The filling down must stop when there is no more data to display in
column
A

='[Master.xls]PL'!$D8 contained in A1
='[Master.xls]PL'!$E8 contained in B1
='[Master.xls]PL'!$F8 contained in C1
='[Master.xls]PL'!$G8 contained in D1
='[Master.xls]PL'!$H8 contained in E1
='[Master.xls]PL'!$I8 contained in F1
='[Master.xls]PL'!$J8 contained in G1

Then I want to save the workbook as a .CSV file

Hope you understand what I am trying to do.


Bob Phillips said:
I think it might help if you give us a hint as to what is going on or
not
as
the case may be, rather than leave us to work it out.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Could someone let me know what is wrong with the following two
pieces
of
code?

Private Sub Test_Click()
Dim LastRow As Long
Dim FirstRow As Long
Dim StartRange As Range

'Find end of data in col A in Sheet1
LastRow = LastDataRow(ThisWorkbook.Sheets("Sheet1").Range("A:A"))
With ThisWorkbook.Sheets("Sheet1")
'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row
Set StartRange = .Range("A1:G1")
End With
StartRange.Resize(LastRow - FirstRow + 1).FillDown
Calculate
ThisWorkbook.Sheets("Sheet1").SaveAs _
Filename:="C:\MyDocumets\Excel examples\ReadyTest.CSV",
FileFormat:=xlCSV
Application.DisplayAlerts = True
Windows("PrepareTest.xls").Activate
Range("D1").Select

Calculate

End Sub


Option Explicit

Function LastDataRow(Col As Range) As Long
'Finds last row containing data in column defined by Col
Dim iCol As Integer
iCol = Col.Column
LastDataRow = Col.Columns(1).Cells(65526).End(xlUp).Row
Do While Cells(LastDataRow, iCol) = 0 Or _
Trim(Cells(LastDataRow, iCol)) = "" _
And LastDataRow > 1
LastDataRow = LastDataRow - 1
Loop
End Function

If it isn't clear what I am trying to achieve I will elaborate further.

Thanks
Pat
 
B

Bob Phillips

You still didn't explain what went wrong, but when I tried it I got stuck
here

'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row

I don't understand your comment. The syntax is wrong, but I don't know what
to put as I don't know what row value are you trying to get here?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Pat said:
Sorry for the vagueness of my question.

The code is placed in a workbook called PrepareTest.xls

I am trying to filldown the following formulas which are on the first row.
The filling down must stop when there is no more data to display in column A

='[Master.xls]PL'!$D8 contained in A1
='[Master.xls]PL'!$E8 contained in B1
='[Master.xls]PL'!$F8 contained in C1
='[Master.xls]PL'!$G8 contained in D1
='[Master.xls]PL'!$H8 contained in E1
='[Master.xls]PL'!$I8 contained in F1
='[Master.xls]PL'!$J8 contained in G1

Then I want to save the workbook as a .CSV file

Hope you understand what I am trying to do.


Bob Phillips said:
I think it might help if you give us a hint as to what is going on or
not
as
the case may be, rather than leave us to work it out.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Pat said:
Could someone let me know what is wrong with the following two pieces of
code?

Private Sub Test_Click()
Dim LastRow As Long
Dim FirstRow As Long
Dim StartRange As Range

'Find end of data in col A in Sheet1
LastRow = LastDataRow(ThisWorkbook.Sheets("Sheet1").Range("A:A"))
With ThisWorkbook.Sheets("Sheet1")
'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row
Set StartRange = .Range("A1:G1")
End With
StartRange.Resize(LastRow - FirstRow + 1).FillDown
Calculate
ThisWorkbook.Sheets("Sheet1").SaveAs _
Filename:="C:\MyDocumets\Excel examples\ReadyTest.CSV",
FileFormat:=xlCSV
Application.DisplayAlerts = True
Windows("PrepareTest.xls").Activate
Range("D1").Select

Calculate

End Sub


Option Explicit

Function LastDataRow(Col As Range) As Long
'Finds last row containing data in column defined by Col
Dim iCol As Integer
iCol = Col.Column
LastDataRow = Col.Columns(1).Cells(65526).End(xlUp).Row
Do While Cells(LastDataRow, iCol) = 0 Or _
Trim(Cells(LastDataRow, iCol)) = "" _
And LastDataRow > 1
LastDataRow = LastDataRow - 1
Loop
End Function

If it isn't clear what I am trying to achieve I will elaborate further.

Thanks
Pat
 
P

Pat

I got stuck at the same location also, the comment refers to PrepareTest.xls
but as the syntax is wrong please ignore where approbate. I am trying to
get the data from Master.xls into A1



Bob Phillips said:
You still didn't explain what went wrong, but when I tried it I got stuck
here

'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row

I don't understand your comment. The syntax is wrong, but I don't know what
to put as I don't know what row value are you trying to get here?

--

HTH

RP
(remove nothere from the email address if mailing direct)


Pat said:
Sorry for the vagueness of my question.

The code is placed in a workbook called PrepareTest.xls

I am trying to filldown the following formulas which are on the first row.
The filling down must stop when there is no more data to display in
column
A

='[Master.xls]PL'!$D8 contained in A1
='[Master.xls]PL'!$E8 contained in B1
='[Master.xls]PL'!$F8 contained in C1
='[Master.xls]PL'!$G8 contained in D1
='[Master.xls]PL'!$H8 contained in E1
='[Master.xls]PL'!$I8 contained in F1
='[Master.xls]PL'!$J8 contained in G1

Then I want to save the workbook as a .CSV file

Hope you understand what I am trying to do.


Bob Phillips said:
I think it might help if you give us a hint as to what is going on or
not
as
the case may be, rather than leave us to work it out.

--

HTH

RP
(remove nothere from the email address if mailing direct)


Could someone let me know what is wrong with the following two
pieces
of
code?

Private Sub Test_Click()
Dim LastRow As Long
Dim FirstRow As Long
Dim StartRange As Range

'Find end of data in col A in Sheet1
LastRow = LastDataRow(ThisWorkbook.Sheets("Sheet1").Range("A:A"))
With ThisWorkbook.Sheets("Sheet1")
'Find start of data based on reference in Sheet1 A1
FirstRow = Range(.[A1].Formula).Row
Set StartRange = .Range("A1:G1")
End With
StartRange.Resize(LastRow - FirstRow + 1).FillDown
Calculate
ThisWorkbook.Sheets("Sheet1").SaveAs _
Filename:="C:\MyDocumets\Excel examples\ReadyTest.CSV",
FileFormat:=xlCSV
Application.DisplayAlerts = True
Windows("PrepareTest.xls").Activate
Range("D1").Select

Calculate

End Sub


Option Explicit

Function LastDataRow(Col As Range) As Long
'Finds last row containing data in column defined by Col
Dim iCol As Integer
iCol = Col.Column
LastDataRow = Col.Columns(1).Cells(65526).End(xlUp).Row
Do While Cells(LastDataRow, iCol) = 0 Or _
Trim(Cells(LastDataRow, iCol)) = "" _
And LastDataRow > 1
LastDataRow = LastDataRow - 1
Loop
End Function

If it isn't clear what I am trying to achieve I will elaborate further.

Thanks
Pat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top