简体   繁体   English

如何让Excel在单元格的开头忽略撇号

[英]How to get Excel to ignore apostrophe in beginning of cell

I'm writing a tool that syncs a simple database with Excel sheets. 我正在编写一个将简单数据库与Excel工作表同步的工具。 Each item in a table in the database corresponds to one row in the worksheet. 数据库中表中的每个项对应于工作表中的一行。 I read the Excel sheet into the tool using C# and the Excel interop com interface, then compared the items' values (ie one of the columns in the excel sheet) after the sync just to make sure that they are equal. 我使用C#和Excel interop com接口将Excel工作表读入工具,然后在同步后比较项目的值(即excel表中的一列),以确保它们相等。

Yesterday I found a case where the comparison wasn't true: 昨天我发现了一个比较不正确的案例:

"'<MedalTitle>' Medal - <MedalDescription>"
"<MedalTitle>' Medal - <MedalDescription>"

The second is the one I've read in from Excel, and as you can see it's skipped the first apostrophe. 第二个是我从Excel中读到的那个,你可以看到它跳过了第一个撇号。 Is there a way to tell Excel to treat the cell as just text (no, just setting the cell's formatting doesn't help)? 有没有办法告诉Excel将单元格视为文本(不,只是设置单元格的格式没有帮助)?

I even tried to copy the value ( 'hello' ) of a cell in VBA like this: 我甚至试图在VBA中复制单元格的值( 'hello' ),如下所示:

Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
   Target.Offset(1, 0).Value = Target.Worksheet.Range("b2").Value
   Target.Offset(2, 0).Value = Target.Worksheet.Range("b2").Formula
   Target.Offset(3, 0).Formula = Target.Worksheet.Range("b2").Formula
   Target.Offset(4, 0).Formula = Target.Worksheet.Range("b2").Value
End Sub

The result was that the value of target cell is always hello' 结果是目标细胞的价值总是你好'

If there is no way, I'll have to do something ugly like 如果没有办法,我将不得不做一些丑陋的事情

if (dbitem.value[0] == ''' )
{
   // stuff
}
else
{
   // regular comparison
}

I'm afraid the apostrophe ' is a special character for Excel when it appears as the first character in a cell as you've found. 我担心撇号'是Excel的一个特殊字符,当它出现在你发现的单元格中的第一个字符时。 It tells Excel to treat the rest of the string as text, so that you can enter something like '34.2 in the cell, and it'll treat it as the string instead of the number (for formatting and so on). 它告诉Excel将字符串的其余部分视为文本,以便您可以在单元格中输入类似'34.2的内容,并将其视为字符串而不是数字(用于格式化等)。

I suggest doing something similar to what you've suggested, except that where you're putting it into Excel, check the first character, and add an extra ' if there's one there already. 我建议你做一些类似于你建议的事情,除了你把它放到Excel中的地方,检查第一个字符,并添加一个额外的'如果已经存在的话。

Alternatively, you could prepend an apostrophe to all values - if you want them all as text that is. 或者,您可以将撇号添加到所有值 - 如果您希望它们全部为文本。 That way you don't need the extra first character check. 这样你就不需要额外的第一个字符检查了。

Look at the PrefixCharacter property of the Range object which corresponds to that cell 查看Range对象的PrefixCharacter属性,该属性对应于该单元格

From the help: 从帮助:

If the TransitionNavigKeys property is False , this prefix character will be ' for a text label, or blank. 如果TransitionNavigKeys属性为False ,则此前缀字符将为“文本标签”或空白。 If the TransitionNavigKeys property is True , this character will be ' for a left-justified label, " for a right-justified label, ^ for a centered label, \\ for a repeated label, or blank. 如果TransitionNavigKeys属性为True ,则此字符将为“左对齐标签”,右对齐标签,^为居中标签,\\为重复标签或空白。

The TransitionNavigKeys part relates to Lotus 1-2-3 compatibility so it's more than likely going to be False TransitionNavigKeys部分与Lotus 1-2-3的兼容性有关,所以它很可能是False

Answer based on article at: 根据以下文章回答:

http://excel.tips.net/Pages/T003332_Searching_for_Leading_Apostrophes.html http://excel.tips.net/Pages/T003332_Searching_for_Leading_Apostrophes.html

(warning: slightly annoying pop-up may appear) (警告:可能会出现稍微恼人的弹出窗口)


edit: actually this probably isn't going to be any use because PrefixCharacter is read-only :( 编辑:实际上这可能不会有任何用处,因为PrefixCharacter是只读的:(

edit2: I was right the first time. edit2:我第一次是对的。 PrefixCharacter only gets populated if the value added to the cell started with ' so just read back PrefixCharacter plus Value and concatenate. PrefixCharacter只有被填充,如果添加到单元格的值开始',所以就先回PrefixCharacterValue和串联。 As long as TransitionNavigKeys is False , that is 只要TransitionNavigKeysFalse ,就是这样

try targetcell.Value instead. 尝试使用targetcell.Value .Formula is the formula seen in the formula bar while .Value is the evaluated value of the cell. .Formula是在公式栏中看到的公式,而.Value是单元格的评估值。

So, I am guessing that you would have used .Formula in your original code as well. 所以,我猜你也会在原始代码中使用.Formula Changing that should work. 改变它应该工作。

EDIT: Ok, it did not work (embarrassed). 编辑:好吧,它不起作用(尴尬)。

Excel treats the starting single quote specially.. so specially that even obscure cell / range properties do not have access. Excel专门处理起始单引号。特别是即使是模糊的单元格/范围属性也没有访问权限。 The only workaround I could find is essentially the same as what you thought initially. 我能找到的唯一解决方法与您最初的想法基本相同。 Here goes: 开始:

If VarType(cell) = 8 And Not cell.HasFormula Then
 GetFormulaI = "'" & cell.Formula
Else
 GetFormulaI = cell.Formula
End If

You might try pre-pending a single quote to your text fields ( '''' + dbField ) in your query so that for fields with embedded single quotes your query would return: 您可以尝试在查询中预先挂起单个引号到文本字段(''''+ + dbField),以便对于具有嵌入单引号的字段,您的查询将返回:

"''stuff in single quotes'"

which when placed in an Excel cell would convert to: 放在Excel单元格中时会转换为:

"'stuff in single quotes'"

for characters that weren't in quotes you would get: 对于没有引号的字符,你会得到:

"'stuff that wasn't in quotes"

which when placed in an Excel cell would convert to: 放在Excel单元格中时会转换为:

"stuff that wasn't in quotes"

Worth a shot. 值得一试。 :-) :-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM