PC Review


Reply
Thread Tools Rate Thread

how to copy a cell multiple times and in many worksheets

 
 
Jerry
Guest
Posts: n/a
 
      3rd Apr 2008
I have a spreadsheet where I need to copy multiple cells every other column
and applicable to other worksheets. Under Set wks = Worksheets("mardet")
I need to put 12 worksheet names (febdet, mardet, aprdet) for the 12 months
of the year. Under Set myRng = .Range("c6:e20,g6:i20") I need to run this
loop 25 times for as to read 3 columns(c thru e), skip one column (f) and do
3 columns (g - i) and skip a column (j) and so on. The code that I use for a
worksheet is below. Your asistance is greatly appreciated

Option Explicit
Sub testme01()

Dim GrpBox As GroupBox
Dim OptBtn As OptionButton
Dim wks As Worksheet
Dim myCell As Range
Dim myRng As Range

Set wks = Worksheets("mardet")

With wks
'nice for testing
.OptionButtons.Delete
.GroupBoxes.Delete

Set myRng = .Range("c6:e20,g6:i20")
For Each myCell In myRng.Cells
With myCell
Set GrpBox = .Parent.GroupBoxes.Add(Top:=.Top, _
Left:=.Left, _
Width:=.Width, _
Height:=.Height)
GrpBox.Caption = ""
GrpBox.Visible = False

Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
Left:=.Left, _
Width:=.Width / 2, _
Height:=.Height)
OptBtn.Caption = ""
OptBtn.LinkedCell = .Address(external:=True)

Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
Left:=.Left + (.Width / 2), _
Width:=.Width / 2, _
Height:=.Height)
OptBtn.Caption = ""

.NumberFormat = ";;;"

End With
Next myCell
End With
End Sub
Sub compliance()

End Sub
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      3rd Apr 2008
First, I'm not sure if I'd do this. This is one heck of a lot of optionbuttons
and groupboxes. When I was testing the code, I broke out of it after a couple
of minutes and a single sheet wasn't done!

But if you want to try (save your work before you start the macro!):

Option Explicit
Sub testme01()

Dim GrpBox As GroupBox
Dim OptBtn As OptionButton
Dim wks As Worksheet
Dim myCell As Range
Dim myRng As Range
Dim mCtr As Long
Dim cCtr As Long
Dim myMonthStr As String

For mCtr = 1 To 1 '12 when you're done testing!
myMonthStr = Format(DateSerial(2008, mCtr, 1), "mmm") & "det"
Set wks = Nothing
On Error Resume Next
Set wks = Worksheets(myMonthStr)
On Error GoTo 0
If wks Is Nothing Then
Set wks = Worksheets.Add
wks.Name = myMonthStr
End If

With wks
'nice for testing
.OptionButtons.Delete
.GroupBoxes.Delete

Set myRng = .Range("c6:c20")
For Each myCell In myRng.Cells
For cCtr = 1 To 100 '100 columns total
If cCtr Mod 4 = 0 Then
'in the fourth column of the group, so skip it
Else
With myCell.Offset(0, cCtr - 1)
Set GrpBox = .Parent.GroupBoxes.Add(Top:=.Top, _
Left:=.Left, _
Width:=.Width, _
Height:=.Height)
GrpBox.Caption = ""
GrpBox.Visible = False

Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
Left:=.Left, _
Width:=.Width / 2, _
Height:=.Height)
OptBtn.Caption = ""
OptBtn.LinkedCell = .Address(external:=True)

Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
Left:=.Left + (.Width / 2), _
Width:=.Width / 2, _
Height:=.Height)
OptBtn.Caption = ""

.NumberFormat = ";;;"

End With
End If
Next cCtr
Next myCell
End With
Next mCtr
End Sub

Jerry wrote:
>
> I have a spreadsheet where I need to copy multiple cells every other column
> and applicable to other worksheets. Under Set wks = Worksheets("mardet")
> I need to put 12 worksheet names (febdet, mardet, aprdet) for the 12 months
> of the year. Under Set myRng = .Range("c6:e20,g6:i20") I need to run this
> loop 25 times for as to read 3 columns(c thru e), skip one column (f) and do
> 3 columns (g - i) and skip a column (j) and so on. The code that I use for a
> worksheet is below. Your asistance is greatly appreciated
>
> Option Explicit
> Sub testme01()
>
> Dim GrpBox As GroupBox
> Dim OptBtn As OptionButton
> Dim wks As Worksheet
> Dim myCell As Range
> Dim myRng As Range
>
> Set wks = Worksheets("mardet")
>
> With wks
> 'nice for testing
> .OptionButtons.Delete
> .GroupBoxes.Delete
>
> Set myRng = .Range("c6:e20,g6:i20")
> For Each myCell In myRng.Cells
> With myCell
> Set GrpBox = .Parent.GroupBoxes.Add(Top:=.Top, _
> Left:=.Left, _
> Width:=.Width, _
> Height:=.Height)
> GrpBox.Caption = ""
> GrpBox.Visible = False
>
> Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
> Left:=.Left, _
> Width:=.Width / 2, _
> Height:=.Height)
> OptBtn.Caption = ""
> OptBtn.LinkedCell = .Address(external:=True)
>
> Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
> Left:=.Left + (.Width / 2), _
> Width:=.Width / 2, _
> Height:=.Height)
> OptBtn.Caption = ""
>
> .NumberFormat = ";;;"
>
> End With
> Next myCell
> End With
> End Sub
> Sub compliance()
>
> End Sub


--

Dave Peterson
 
Reply With Quote
 
Jerry
Guest
Posts: n/a
 
      3rd Apr 2008
Dave:
I copied the modified code and it works for the 1st worksheet. It took over
30 minutes to fill in the cells in one of the worksheets, with the old code
it took 3secs to fill in a block of 3 cols (C6:E20). I am thinking there is
loop that is slowing the process.

"Jerry" wrote:

> I have a spreadsheet where I need to copy multiple cells every other column
> and applicable to other worksheets. Under Set wks = Worksheets("mardet")
> I need to put 12 worksheet names (febdet, mardet, aprdet) for the 12 months
> of the year. Under Set myRng = .Range("c6:e20,g6:i20") I need to run this
> loop 25 times for as to read 3 columns(c thru e), skip one column (f) and do
> 3 columns (g - i) and skip a column (j) and so on. The code that I use for a
> worksheet is below. Your asistance is greatly appreciated
>
> Option Explicit
> Sub testme01()
>
> Dim GrpBox As GroupBox
> Dim OptBtn As OptionButton
> Dim wks As Worksheet
> Dim myCell As Range
> Dim myRng As Range
>
> Set wks = Worksheets("mardet")
>
> With wks
> 'nice for testing
> .OptionButtons.Delete
> .GroupBoxes.Delete
>
> Set myRng = .Range("c6:e20,g6:i20")
> For Each myCell In myRng.Cells
> With myCell
> Set GrpBox = .Parent.GroupBoxes.Add(Top:=.Top, _
> Left:=.Left, _
> Width:=.Width, _
> Height:=.Height)
> GrpBox.Caption = ""
> GrpBox.Visible = False
>
> Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
> Left:=.Left, _
> Width:=.Width / 2, _
> Height:=.Height)
> OptBtn.Caption = ""
> OptBtn.LinkedCell = .Address(external:=True)
>
> Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
> Left:=.Left + (.Width / 2), _
> Width:=.Width / 2, _
> Height:=.Height)
> OptBtn.Caption = ""
>
> .NumberFormat = ";;;"
>
> End With
> Next myCell
> End With
> End Sub
> Sub compliance()
>
> End Sub

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      3rd Apr 2008
There's a couple of loops (and you missed a change to the code):

For mCtr = 1 To 1 '12 when you're done testing!
becomes
For mCtr = 1 To 12

And you're adding lots and lots more objects. That why I warned you.

Jerry wrote:
>
> Dave:
> I copied the modified code and it works for the 1st worksheet. It took over
> 30 minutes to fill in the cells in one of the worksheets, with the old code
> it took 3secs to fill in a block of 3 cols (C6:E20). I am thinking there is
> loop that is slowing the process.
>
> "Jerry" wrote:
>
> > I have a spreadsheet where I need to copy multiple cells every other column
> > and applicable to other worksheets. Under Set wks = Worksheets("mardet")
> > I need to put 12 worksheet names (febdet, mardet, aprdet) for the 12 months
> > of the year. Under Set myRng = .Range("c6:e20,g6:i20") I need to run this
> > loop 25 times for as to read 3 columns(c thru e), skip one column (f) and do
> > 3 columns (g - i) and skip a column (j) and so on. The code that I use for a
> > worksheet is below. Your asistance is greatly appreciated
> >
> > Option Explicit
> > Sub testme01()
> >
> > Dim GrpBox As GroupBox
> > Dim OptBtn As OptionButton
> > Dim wks As Worksheet
> > Dim myCell As Range
> > Dim myRng As Range
> >
> > Set wks = Worksheets("mardet")
> >
> > With wks
> > 'nice for testing
> > .OptionButtons.Delete
> > .GroupBoxes.Delete
> >
> > Set myRng = .Range("c6:e20,g6:i20")
> > For Each myCell In myRng.Cells
> > With myCell
> > Set GrpBox = .Parent.GroupBoxes.Add(Top:=.Top, _
> > Left:=.Left, _
> > Width:=.Width, _
> > Height:=.Height)
> > GrpBox.Caption = ""
> > GrpBox.Visible = False
> >
> > Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
> > Left:=.Left, _
> > Width:=.Width / 2, _
> > Height:=.Height)
> > OptBtn.Caption = ""
> > OptBtn.LinkedCell = .Address(external:=True)
> >
> > Set OptBtn = .Parent.OptionButtons.Add(Top:=.Top, _
> > Left:=.Left + (.Width / 2), _
> > Width:=.Width / 2, _
> > Height:=.Height)
> > OptBtn.Caption = ""
> >
> > .NumberFormat = ";;;"
> >
> > End With
> > Next myCell
> > End With
> > End Sub
> > Sub compliance()
> >
> > End Sub


--

Dave Peterson
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy same cell in multiple worksheets onto one worksheet Tia Microsoft Excel Misc 1 17th Sep 2009 12:40 AM
Copy Cell Multiple times depending on variable tbarnes@cstone-ep.com Microsoft Excel Discussion 5 10th Dec 2008 01:15 PM
Copy rows multiple times changing value of a cell in one column each time EE Microsoft Excel Programming 1 11th Oct 2007 01:13 PM
I wnat to copy several Worksheets, Several Times... =?Utf-8?B?RHIuIERhcnJlbGw=?= Microsoft Excel Programming 13 2nd Aug 2007 11:20 AM
how do i copy the same cell multiple times in excel. =?Utf-8?B?YW5keQ==?= Microsoft Excel Misc 1 7th Jul 2006 12:50 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:17 AM.