![]() |
|
#1
|
|||
|
|||
![]()
Hi.
I'm working with Excel 2010 to make a game tool for calculating stats. The idea is to have most of the worksheet protected with unprotected blocks of cells for inputting values, with separate protected blocks of cells for calculated returns and compiling. Everything was going very well until I decided that copypasting csv text was necessary. Importing is not an option. I recorded macros that did the job, but they don't function on a password protected sheet. I tried adding password logging to the macros, but they are hit and miss, firing incorrectly and leaving the sheet sometimes protected, sometimes not, and sometimes not recognizing it's own password. The second problem I'm having is that when the button that fires the macro is clicked, if there is no data in the selected cells, it dumps into debug and leaves the worksheet unprotected. I tried various way to correct these problems with the results above. This tool is intended to be distributed to fellow gamers, most of whom will have little idea how to treat it delicately. Currently, I'm saving it as a template, hoping that I could make it bulletproof, but that really didn't seem to accomplish much or stop me from saving. Help would be appreciated. Here's the macro I'm having trouble with. I have seven or so (named differently, of course) that do the exact same job of handling csv copypaste in different areas of the form. Code:
Sub CSVarmor() ' ' CSVarmor Macro ' paste and click csv to armor ' ' Application.ScreenUpdating = False Dim sh As Worksheet For Each sh In ActiveWorkbook.Worksheets sh.Unprotect Password:=jtls Next sh Range("C5:C14").Select Selection.TextToColumns Destination:=Range("C5"), DataType:=xlDelimited, _ TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _ Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _ :=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), TrailingMinusNumbers:= _ False Range("E15").Select For Each sh In ActiveWorkbook.Worksheets sh.Protect Password:=jtls Next sh Application.ScreenUpdating = True End Sub |
#2
|
|||
|
|||
![]() Quote:
Code:
If WorksheetFunction.CountA(.Range("C5:C14")) = 0 Then Exit Sub Quote:
I would only unprotect and re-protect the sheet being worked on unless there's more to this than what I' seeing. Along the lines of this Code:
Sub CSVarmor() ' ' CSVarmor Macro ' paste and click csv to armor ' Application.ScreenUpdating = False With ActiveSheet If WorksheetFunction.CountA(.Range("C5:C14")) = 0 Then Exit Sub .Unprotect Password:="jtls" .Range("C5:C14").TextToColumns Destination:=.Range("C5"), _ DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _ Comma:=False, Space:=False, Other:=False, _ FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _ TrailingMinusNumbers:=False .Protect Password:="jtls" End With Application.ScreenUpdating = True End Sub |
#3
|
|||
|
|||
![]()
This looks exceptional. Code is much cleaner than the editor generated spaghetti and the copypaste code I spliced in there. Protecting and unprotecting just the worksheet seems more sensible.
The macros were done by "recording" and are saved to the worksheet. Buttons were created from the developer tab, controls menu, "insert" dropdown, and are placed in a relatively handy spot on the same worksheet as the whole operation is happening. Unprotecting just the worksheet will be fine. If there is ever a second sheet, it'll be different tools. A quick question about: FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), This was recording generated and I'm not sure of its function. This "may" correspond to 4 cells where i input delimated junk data in the first column of the intended array, so the function would work as I recorded it. My concern is that this array should actually be (overall) 3 columns by 10 rows, and that my other similar arrays (and the associated macros) all should be various numbers of columns by 10 rows... but the array statements in the macros all seem to have no logical correlation UNLESS it was defined by which cells had junk data. Should I edit the array statements up to (10, 1), (the column I intend to paste the csv data)? Should I define the multiple columns of the input array for csv pasting, as well? Will this prevent attempts to paste too many csv values? Thanks for your help... |
#4
|
|||
|
|||
![]()
...also
Code:
If WorksheetFunction.CountA(.Range("C5:C14")) = 0 Then Exit Sub |
#5
|
|||
|
|||
![]() Quote:
but if I reduce the generated line to just this things still seem to work as I would expect so I wouldn't be too concerned. Code:
.Range("C5:C14").TextToColumns Destination:=Range("C5"), DataType:=xlDelimited, Semicolon:=True If you run into issues, attach a sample workbook to investigate. |
#6
|
|||
|
|||
![]()
So it's (row, column) rather than (column, row)? As in: (Array(row, column), ? My junk data had too many semicolons. Interesting. If that's the case I might fill it out for each macro to indicate each specific number of columns per array. It might be messier, but I'm trying to stop things from breaking in creative ways.
You seem to have solved all my problems. Thank you. I "do" have one other question! ![]() Code:
.Range("C5:C14").TextToColumns Destination:=.Range("C5"), _ DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _ ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, _ Comma:=False, Space:=False, Other:=False, _ FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1)), _ TrailingMinusNumbers:=False IE: 100;;100; = 100 (null cell) 100 I'd rather idiot proof it by having the function see a particular character as a null value and skip the cell. IE: 100;z;100; = 100 (null cell) 100 This would allow me to turn back on the preference to see extra semicolons as just "fatfingering" and ensure that the user truly intended to skip over a value. Is a parameter like this possible? |
#7
|
|||
|
|||
![]()
The text to column dialogue has a check box next to the delimiter choices that is Treat consecutive delimiters as one
When checked it adds ConsecutiveDelimiter:=True into the line of code. |
#8
|
|||
|
|||
![]()
Thanks for all the help! I have to relearn everything for every occasional project. Cheers, sir.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Sorting in protected worksheet | john64 | Excel | 2 | 12-31-2016 12:04 PM |
![]() |
AHB | Excel Programming | 2 | 02-27-2012 10:25 AM |
![]() |
udea | Excel | 1 | 02-06-2012 07:43 PM |
![]() |
Platform | Mail Merge | 10 | 09-28-2011 05:12 AM |