Macro problem! NEED HELP!!!

G

Guest

Guest
Dear Mr. Excel,

I have a problem with a macro I have written.

Sub Start()
Sheets(1).Cells(3, 1).CurrentRegion.Copy
Sheets(2).Cells(6, 1).PasteSpecial xlValues
Cells(6, 3).FormulaR1C1 = "=SUMIF(Kelder!C,RC[-2],Kelder!C[2])"
Cells(6, 3).Copy
m = ActiveSheet.UsedRange.Rows.Count
For x = 6 To m
Cells(x, 3).PasteSpecial
Next
Application.CutCopyMode = False
End Sub

The formula contains the word “Kelder” because that’s the name of the active sheet.
If I change the name of the sheet, the macro doesn’t work anymore.
The name of the sheet varies.
How can I create a macro who works regardless the name of the sheet
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
On 2002-02-18 05:54, Anonymous wrote:
Dear Mr. Excel,

I have a problem with a macro I have written.

Sub Start()
Sheets(1).Cells(3, 1).CurrentRegion.Copy
Sheets(2).Cells(6, 1).PasteSpecial xlValues
Cells(6, 3).FormulaR1C1 = "=SUMIF(Kelder!C,RC[-2],Kelder!C[2])"
Cells(6, 3).Copy
m = ActiveSheet.UsedRange.Rows.Count
For x = 6 To m
Cells(x, 3).PasteSpecial
Next
Application.CutCopyMode = False
End Sub

The formula contains the word “Kelder” because that’s the name of the active sheet.
If I change the name of the sheet, the macro doesn’t work anymore.
The name of the sheet varies.
How can I create a macro who works regardless the name of the sheet

Is the worksheet always in the same position (i.e., it's always the first worksheet)? If so, you could use:
Code:
Sub Start()
Dim SheetName As String

Sheets(1).Cells(3, 1).CurrentRegion.Copy
SheetName = Sheets(1).Name
Sheets(2).Cells(6, 1).PasteSpecial xlValues
Cells(6, 3).FormulaR1C1 = "=SUMIF(" & SheetName & "!C,RC[-2]," & _
    SheetName & "!C[2])"
Cells(6, 3).Copy
m = ActiveSheet.UsedRange.Rows.Count
For x = 6 To m
Cells(x, 3).PasteSpecial
Next
Application.CutCopyMode = False
End Sub

PS - you should post this question in the Macro Forum :)

_________________

Barrie Davidson
My Excel Web Page
This message was edited by Barrie Davidson on 2002-02-18 05:59
 
Upvote 0

Forum statistics

Threads
1,214,377
Messages
6,119,183
Members
448,872
Latest member
lcaw

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