copying cells through data

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Copying cells through data validation

Copying cells through data validation

resolvedResolved · Medium Priority · Version 2007

Edited on Tue 10 Sep 2013, 13:08

Hugh has attended:
Excel VBA Intro Intermediate course

Copying cells through data validation

Hi,

I'm running into trouble with some code I'm using. Essentially, the code uses data validation to allow a user to filter out information from a master worksheet that is then presented in a table on a separate sheet.

It filters through names in column A fine and then themes of survey Questions from column C. It then pastes the dates of the survey from column E onto the new sheet. But it isn't copying the score for the specific questions (which the user has checked) across from the date as expected.

The code for the module is below:

Sub RepTracker()

Dim lastrow As Long, lngCol As Long, chkSum As Long, i As Long
Dim LAname As String, ThemeSelect As String

Application.ScreenUpdating = False

With Sheet1

'// set the filter criteria to string variables based on drop down selections
LAname = .Range("B1").Value
ThemeSelect = Range("B2").Value
chkSum = WorksheetFunction.CountA(.Range("D3:AS3"))

'// clear the output cells to receive new data - -- adjust range if required
.Range("C6:U500").ClearContents


End With

'// confirm none of the selections are blank before applying filter and that at least one survey question is picked

Select Case True
Case LAname = "": MsgBox "Local Authority not selected", vbExclamation: Exit Sub
Case ThemeSelect = "": MsgBox "Theme not selected", vbExclamation: Exit Sub
Case chkSum < 1: MsgBox "At least one question must be selected", vbExclamation: Exit Sub
End Select


'// find last data row and column matching the hole selection then apply filter and copy results to sheet1

With Sheet4
lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
.AutoFilterMode = False
.Range("A1:AU" & lastrow).AutoFilter Field:=1, Criteria1:=LAname
.Range("A1:AU" & lastrow).AutoFilter Field:=3, Criteria1:=ThemeSelect
.Range("E1:E" & lastrow - 1).Offset(1, 0).SpecialCells(12).Copy Sheet1.Range("C6")

On Error Resume Next
For i = 4 To 45
If Sheet1.Cells(3, i) = "a" Then
lngCol = Sheet1.Cells(2, i).Value + 4
End If
.Range(.Cells(1, lngCol), .Cells(lastrow - 1, lngCol)).Offset(1, 0).SpecialCells(12).Copy Sheet1.Cells(6, lngCol - 1)
Next i

End With

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub



And there is an additional private sub for Worksheet1:

Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If Target.Cells.Count > 1 Then Exit Sub

If Not Intersect(Target, Range("D3:AS3")) Is Nothing Then
Target.Font.Name = "Marlett"
If Target = vbNullString Then
Target = "a"
Else
Target = vbNullString
End If
End If

End Sub


The main module code runs into an error on the line:

.Range("E1:E" & lastrow - 1).Offset(1, 0).SpecialCells(12).Copy Sheet1.Range("C6")

There is an error 1004: range of worksheet object is not defined.

Any idea what is going wrong?

Thanks,

Hugh

RE: Copying cells through data validation

Hi Hugh

Thanks for getting in touch.

I would recommend trying to make dummy versions of that line without variables in, e.g.

Sheet4.Range(Sheet4.Cells(1, 10), .Cells(30, 10)).Offset(1, 0).SpecialCells(12).Copy

And see if that works.

If the syntax of the line is correct, I'd recommend trying it again but with the Locals window turned on (View > Locals). Remove the ScreenUpdating and step through it to watch the values change.

What values are giving it a problem?

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: Copying cells through data validation

Thanks for your reply Gary.

I tried replacing the line of code with your suggestion, and it copied column ten for the two initial variables selected on B1 and B2 of the report sheet. But what I want is for the date (column E) to be copied over from the main database to the report sheet and columns of survey scores underneath whichever boxes are checked between D3:AS3.

If it helps, I have used the code from this forum:

http://www.excelforum.com/excel-charting-and-pivots/807056-creating-a-chart-from-a-userform.html

It was initially designed for showing golf scores depending on the player and course selected (Local authority and theme in my version) and the hole checked (survey question in mine).

I have hardly changed the code at all, just the variables and ranges so it is likely something in the line I mentioned before relates specifically to the original sheet.

When I ran through the code line by line, as you suggested, it is saying 'no cells were found' for the line:

Range("E1:E" & lastrow - 1).Offset(1, 0).SpecialCells(12).Copy Sheet1.Range("C6")

But there are values there. I'm guessing the problem has something to do with specialcells or the failure of the code to register which boxes between D3:AS3 have been checked.

RE: Copying cells through data validation

Hi Hugh

Thanks for your reply. The SpecialCells property with a value of 12 ensures only visible cells are copied (not hidden rows).

I can't quite see what the problem is without more context. Would you be able to send me a copy of the workbook? Send it to gary@stl-training.co.uk.

Kind regards

Gary Fenn
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Using Excel's MODE function

Use Excel's MODE function to display the most common value present in a particular range of cells.

The Mode function looks like this:

=MODE(cell range)

As an example, if 35 is the most commonly recurring number in a particular cell range, then the function will display 35.

View all Excel hints and tips


Server loaded in 0.08 secs.